HTM L forms variables

HTM L forms variables

One of the most common ways in which variable information is delivered is through HTML forms.

Appendix A present s det ailed inform at ion on creat ing HTML form s. Refer t o t hat appendix before you read t his sect ion if you are unfam iliar w it h

XREF t his t opic.

For each of your form elements you will have to assign a name and a value attribute. When the form is submitted, the name=value pairs are passed to PHP. They can be passed to PHP by either the GET or POST methods, depending on what you chose in your form action attribute.

Once a form is submitted, the form elements automatically become global vari- ables in PHP. (Global variables and variable scope are discussed in Chapter 6). It is truly a no-muss, no-fuss way of doing business. Consider the following simple HTML form:

<form action=mypage.php action=post>

78 Part II: Working with PHP

<input type=text name=first_name> <input type=submit name=submit value=add>

</form> Once the user hits the submit button, variables named $email , $first_name ,

and $submit will be available in the called PHP page. You can then process these variables however you see fit. Note that in most of our applications we will be using the value of the submit button, to make sure the page understands what action the user has taken. The following is a brief example of how this will work. Assume the name of the page is mypage.php.

<?php if (isset($submit) && $submit==”yes”) {

echo “thank you for submitting your form.” } else { ?> <form action=mypage.php action=post>

<input type=text name=email> <input type=text name=first_name> <input type=submit name=submit value=yes>

</form> <?php } ?>

N OTE

In some browsers if there is only one submit button within a form, the user can hit the enter key and submit the form without the submit button infor- mation being sent.

On his or her first visit to this page the user will be presented with a form. Once the form is submitted and the page recalls itself with the new variable information, only the thank you message will appear.

Form variables will also be accessible through either the $HTTP_POST_VARS or $HTTP_GET_VARS array, depending on the method used in your form. These are convenient if you have variables coming from both methods, if variables from forms could carry the same name as variables in your script, or if you have an undefined set of variables being passed and you need to check what’s there.

Chapter 4: Getting Started with PHP —Variables

If you are wondering when you might have to deal with variables from both GET and POST, consider a situation where a user gets to a page by clicking on a link with querystring information. The user may then end up at a page with a form. If the action of form is an empty string, the form will submit to itself and it will main- tain the querystring If the method of the form is POST, variables will be coming from both GET and POST

You can access any individual element like any associative array ($HTTP_ POST_VARS[“e-mail”]). Or you can loop through all of the contents of the array as follows:

while (list($key, $value) = each($HTTP_POST_VARS)) {

echo “variable = $key value = $value <br>”; }