HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [208]
♦ Each check box has an individual name. Check boxes are a little bit different. Each check box has its own name, but the value is sent to the server only if the check box is checked.
I don’t cover text areas, passwords fields, or hidden fields here because to PHP, they are just like text boxes. Retrieve data from these elements just like you do for text fields.
Responding to a complex form
The monty.php program is designed to respond to monty.html. You can see it respond when I submit the form in monty.html, as shown in Figure 2-6.
It’s no coincidence that monty.html uses monty.css and calls monty.php. I deliberately gave these files similar names so it will be easy to see how they fit together.
Figure 2-6: The monty.php program responds to the Monty Python quiz.
This program works like most PHP programs: It loads data from the form into variables and assembles output based on those variables. Here’s the PHP code:
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
Monty Python quiz results
//gather the variables
$name = $_REQUEST[“name”];
$quest = $_REQUEST[“quest”];
//don’t worry about check boxes yet; they may not exist
//send some output
$reply = <<< HERE
Your name is $name.
Your quest is $quest.
HERE;
print $reply;
//determine if she’s a witch
$witch = false;
//See if check boxes exist
if(isset($_REQUEST[“nose”])){
$witch = true;
}
if(isset($_REQUEST[“hat”])){
$witch = true;
}
if(isset($_REQUEST[“newt”])){
$witch = true;
}
if ($witch == true){
print “
She’s a witch!
\n”;} // end if
?>
If you want to respond to a form with multiple types of data, here’s how it’s done:
1. Begin with the XHTML form.
Be sure you know the names of all the fields in the form because your PHP program will need this information.
2. Embed your PHP inside an XHTML framework.
Use your standard XHTML framework as the starting point for your PHP documents, too. The results of your PHP code should still be standards-compliant XHTML. Use the symbols to indicate the presence of PHP code.
3. Create a variable for each form element.
Use the $_REQUEST technique described in the “Receiving data in PHP” section of this chapter to extract form data and store it in local variables:
//gather the variables
$name = $_REQUEST[“name”];
$quest = $_REQUEST[“quest”];
Don’t worry about the check boxes yet. Later on, you’ll determine whether they exist. You don’t really care about their values.
4. Build your output in a heredoc.
PHP programming almost always involves constructing an XHTML document influenced by the variables that were extracted from the previous form. The heredoc method (described in Chapter 1 of this minibook) is an ideal method for packaging output:
//send some output
$reply = <<< HERE
Your name is $name.
Your quest is $quest.
HERE;
print $reply;
5. Check for the existence of each check box.
Check boxes are the one exception to the “treat all form elements the same way” rule of PHP. The important part of a check box isn’t really its value. What you really need to know is whether the check box is checked. Here’s how it works: If the check box is checked, a name and value are passed to the PHP program. If the check box is not checked, it’s like the variable never existed:
a. Create a variable called $witch set to false. Assume innocent until proven guilty in this witch hunt.
Each check box, if checked, would be proof that she’s a witch. The isset() function is used to determine whether a particular variable