1.3. Processing Information from the Form

Say that Joe Customer fills in an HTML form, selecting from lists and typing information into text fields. When he clicks the submit button, the script listed in the action attribute of the <form> tag, such as action=process_form.php, runs and receives the information from the form. Handling form information is one of PHP's best features. You don't need to worry about the form data — just get it from one of the built-in arrays and use it.

The form data is available in the processing script in arrays, such as $_POST or $_GET. The key for the array element is the name of the input field in the form. For instance, if you echo the following field in your form

echo "<input type='text' name='firstName' />";

the processing script can use the variable $_POST[firstName], which contains the text that the user typed into the field. The information that the user selects from drop-down lists or radio buttons is similarly available for use. For instance, if your form includes the following list of radio buttons,

echo "<input type='radio' name='pet' value='dog' />dog\n";
echo "<input type='radio' name='pet' value='cat' />cat\n";

you can access the variable $_POST[pet], which contains either dog or cat, depending on what the user selected.

You handle check boxes in a slightly different way because the user can select more than one check box. As shown in Listing 1-8, the data from a list of check boxes can be stored in an array so that all the check boxes ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.