Processing a Form with PHP
Now let's look at how each type of item in a form is handled by PHP after the submit button is clicked.
Accessing Form Values
Form values are made available in PHP by using some special array structures. The arrays $_GET and $_POST contain values submitted using the GET and POST methods, respectively. A hybrid array, $_REQUEST, contains the contents of both of these arrays, as well as the values from $_COOKIE, which you will use in Lesson 14, "Cookies and Sessions."
| Super-globals The system-generated arrays that have names beginning with an underscore character are known as super-globals because they can be referenced from anywhere in a PHP script, regardless of scope. For instance, you do not need to explicitly declare $_POST as global to access its elements within a function. |
| Default Check Box Values If you do not specify a VALUE attribute for a check box item, its value in PHP when checked is on. |
This is a useful debugging technique if you want to see exactly what data is being passed to a script from a form. If you create send_comments.php, containing just these lines, the output shows you the value of each form element in turn. The following is sample output:
echo "<PRE>";
print_r($_POST);
echo "</PRE>";
Even the value of a submit button can be seen by PHP if the button is given a name and the button is clicked when the form is submitted. The following form has two buttons with different names, so that you can use PHP to determine which button was actually clicked:
Array
(
[name] => Chris Newman
[email] => chris@lightwood.net
[gender] => m
[referrer] => search
[may_contact] => Y
[comments] => This is my favorite website ever
)
In button.php, you could use a condition similar to the following to see which button is clicked:
<FORM ACTION="button.php" METHOD=POST>
<INPUT TYPE="SUBMIT" NAME="button1" VALUE="Button 1">
<INPUT TYPE="SUBMIT" NAME="button2" VALUE="Button 2">
</FORM>
The VALUE attribute of a submit button determines what label appears on the button itself, but that value is also the value that is passed to PHP when the button is clicked.
if (isset($_POST["button1"])) {
echo "You clicked button 1";
}
elseif (isset($_POST["button2"])) {
echo "You clicked button 2";
}
else {
echo "I don't know which button you clicked!";
}
Hidden Inputs
One other type of form input is available, and it can be used to pass values between scripts without their being visible on the web page itself. The HIDDEN type takes NAME and VALUE attributes, as usual, but it simply acts a placeholder for that value.The following hidden input is passed to the PHP script when the form is submitted, and $_POST["secret"] contains the value from the form:
Be aware, however, that HIDDEN attribute inputs are not secure for transmitting passwords and other sensitive data. Although they do not appear on the web page, if you view the page source, you can still see hidden values in the HTML code.
<INPUT TYPE="HIDDEN" NAME="secret" VALUE="this is a secret">