3PHP
3PHP
3PHP
Lecture 3
2021-2022
E-mail: [email protected]
Computer science Dept_UOD
23 April 2015
OUTLINES
PHP Forms and User Input
2
FORM
It is used to collect data from the user.
<html><body>
<label for="Name">Name:</label>
<label for="Age">Age:</label>
<p> <input type="text" id="Age" name="age" /></p>
</form>
4
</body></html>
In the example HTML page above when the user fills in this form and
click on the submit button, the form data is sent to the "1welcome.php"
file.
Welcome Azad.
5
You are 22 years old.
PHP FORMS AND USER INPUT
There are two attributes to the <form> tag that you should be
aware of and use: action and method.
Action sets the location of the page that will handle the
results of the form the place where the variables should be
sent.
7
PHP $_POST
The $_POST variable is used to collect values from a form with
method="post".
8
RETRIEVE MULTIPLE VALUES WITH PHP
when we working with SELECT elements. These elements make it
possible for the user to choose multiple items, like that
9
SELECT ELEMENT
Example2: Select Element
<body>
<label for=" products "> Select some products:</:</label>
<p>
<select name= "products[]" multiple>
<option value= " Motherboard "> Motherboard</option>
<option value= "CD-ROM "> CD-ROM </option>
<option value= "Monitor "> Monitor </option>
<option value= "Keyboard"> Keyboard </option>
</select>
</p>
</body> 10
RETRIEVE SELECT VALUES WITH PHP
<body>
<label for="name">Client Side Web Programming </label>
<p>
<input type=" checkbox " name="WebProgram[]" value=“html">
HTML <br>
<input type=" checkbox " name="WebProgram[]" value =“php">
PHP <br>
<input type=" checkbox " name =" WebProgram[]" value =“css">
CSS <br> </p>
12
</body>
RETRIEVE CHECKBOX VALUES WITH PHP
echo "<p> Client side Markup Language are : <br>";
if(!empty($_POST['WebProgram ']))
echo"<ul>";
echo"<li>$value</li>";
echo"</ul>";
}
13
COMBINING HTML AND PHP CODE ON A SINGLE PAGE
Example4: HTML and PHP Code on a same page
<html><body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<label for="Name">Name:</label>
<p> <input type="text" id="name" name="name" /> </p>
<label for="Age">Age:</label>
<p> <input type="text" id="Age" name="age" /></p>
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST['submit'])){
echo "Welcome ". $_POST['name'].".<br >";
echo "You are ". $_POST['age'] ." years old."; } 14
?></body></html>
OUTPUT
Figure3: DataList 16
PHP GLOBAL VARIABLES - SUPERGLOBALS
Several predefined variables in PHP are "superglobals", which
means that they are always accessible in all scopes and you
can access them from any function, class or file without having
to do anything special.
The PHP superglobal variables are:
1. $GLOBALS
2. $_SERVER
3. $_REQUEST
4. $_POST
5. $_GET
6. $_FILES
7. $_COOKIE
8. $_SESSION 17
PHP $GLOBALS
$GLOBALS is a PHP super global variable which is used to
access global variables from anywhere in the PHP script (also
from within functions or methods).
18
PHP $GLOBALS
Example5: Super global variable $GLOBALS
<?php
$a=22;
$b=3;
function add() {
$GLOBALS[‘c']= $GLOBALS[‘a']+ $GLOBALS[‘b'];
} add();
echo $c;
?>
Element/Code Description
20
PHP $_SERVER
Example6: Super global variable $server
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['SERVER_PORT'];
echo "<br>";
echo $_SERVER['REQUEST_METHOD'];
?>
21
PHP $_REQUEST
PHP $_REQUEST is used to collect data after submitting an
HTML form.
22
PHP $_REQUEST
Example7: Super global variable $GLOBALS
<html><body>
<label for="Name">Name:</label>
24
PHP $_POST
Example8: Super global variable $GLOBALS
<html><body>
<label for="Name">Name:</label>
26
PHP $_GET
Example9: Assume we have an HTML page that contains a hyperlink with
parameters:
<html><body>
</body> </html>
When a user clicks on the link "Test $GET", the parameters "subject" and
"web" are sent to "test_get.php", and you can then access their values in
"test_get.php" with $_GET.
<html><body><?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web']; 27
?></body></html