HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [207]
If you don’t care whether the data was sent via get or post, use $_REQUEST:
$userName = $_REQUES[“userName”];
The $_REQUEST superglobal grabs data from both get and post requests, so it works, no matter how the form was encoded. Many programmers use the $_REQUEST technique because then they don’t have to worry about the encoding mechanism.
If you don’t like the idea of somebody accessing your data without a form, use $_POST in your PHP program. If data is encoded in the URL, your program ignores it because you’re only responding to post data, and data encoded in the URL is (by definition) get data.
This solution is far from foolproof. There’s nothing to prevent a bad guy from writing his own form using the post method and passing data to your program that way. You can never be 100 percent safe.
Retrieving Data from Other Form Elements
It’s just as easy to get data from drop-down lists and radio buttons as it is to get data from text fields. In PHP (unlike JavaScript), you use exactly the same technique to extract data from any type of form element.
Building a form with complex elements
For an example of a more complex form, look over monty.html in Figure 2-5. This program is a tribute to my favorite movie of all time. (You might just have to rent this movie if you’re really going to call yourself a programmer. It’s part of the culture.)
Figure 2-5: The Monty Python quiz features a drop-down list, radio buttons, and check boxes (and a newt).
The XHTML form poses the questions. (Check out Book I, Chapter 7 for a refresher on XHTML forms, if you need it.) Here’s the code:
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
type = ”text/css”
href = ”monty.css” />
Monty Python Quiz
There’s nothing too crazy about this code. Please note the following features:
♦ The action attribute is set to monty.php. This page (monty.html) will send data to monty.php, which should be in the same directory on the same server.
♦ The method attribute is set to post. All data on this page will be passed to the server via the post method.
♦ Each form element has a name attribute. The name attributes will be used to extract the data in the PHP program.
♦ All the radio buttons have the same name value. The way you get radio buttons to work together is to give them all the same name. And although they all have the same name, each has a different value. When the PHP program receives the request, it will get