- Creating a Simple Form
- Using GET or POST
- Receiving Data from a Form in PHP
- Displaying Errors
- Error Reporting
- The Register Globals Problem
- Manually Sending Data to a Page
Receiving Data from a Form in PHP
Now that you've created a basic HTML form, you need to write the handle_form.php script that will receive and process the data generated by the feedback.html page. For this example, the PHP script will simply repeat what the user entered into the form. In later chapters, you'll learn how to take this information and store it in a database or send it in an email.
For many readers, this example will be surprisingly simple, and the script will work flawlessly. But some may encounter a small hiccup or two, thanks to major changes in PHP's default configuration. In many PHP installations, you can access form values by taking the name attribute of the various inputs and adding a dollar sign to create the variable. In this example, those variables would be $title, $name, $email, $response, $comments, and $submit.
You'll write the script using this standard method, which will work for many readers. If it doesn't, then this leads nicely into the debugging techniques covered next. So, as a heads up, if you see unexpected results with this example, fear not: The next three sections of the chapter explain everything and teach some valuable lessons in the process.
To create the PHP script:
- Open your text editor and create a new document (Script 3.4):
<!DOCTYPE html PUBLIC" -//W3C// → DTD XHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/ → xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/ → xhtml"> <head> <meta http-equiv="content-type" →content="text/html; charset= →iso-8859-1" /> <title>YourFeedback</title> </head> <body>
Script 3.4. By taking the value of the name elements in the HTML form and adding a dollar sign, you create variables that contain the values the user entered in the corresponding form fields.
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="content-type" content="text/html; charset= iso-8859-1" /> 6 <title>Your Feedback</title> 7 </head> 8 <body> 9 <?php // Script 3.4 - handle_form.php 10 11 // This page receives the data from feedback.html. 12 // It will receive: title, name, email, response, comments, and submit. 13 14 print "Thank you $title $name for your comments. <br />"; 15 print "You stated that you found this example to be $response and added: $comments"; 16 ?> 17 </body> 18 </html>
Insert the opening PHP tag and add any comments:
<?php // Script3.4-handle_form.php // This page receives the data from →feedback.html. // It will receive: title, name, email, →response, comments, and submit.
You add comments to the script to remind yourself of its purpose. Even though the feedback.html page indicates where the data is sent (via the action attribute), a comment here indicates the reverse (from where this script is getting its data).
Print out the user information:
print "Thank you $title $name for your → comments.<br/>"; print "You stated that you found this → example to be $response and added: → $comments";
Again, to access what the user typed into the Name input box, you refer to the $name variable. The same applies to all the other form inputs, regardless of their type.
- Close the PHP section and complete the HTML page.
?> </body> </html>
- Save the script as handle_form.php.
- Upload the script to the server (or store it in the proper directory on your computer if you've installed PHP), making sure it's saved in the same directory as feedback.html.
Test the script in your Web browser by going to feedback.html and then submitting the form (Figures 3.8 and 3.9).
Figure 3.8 Whatever the user enters into the HTML form should be printed out to the Web browser by the handle_form.php script (see Figure 3.9).
Figure 3.9 This is another application of the print statement discussed in Chapter 1, but it constitutes your first dynamically generated Web page.
If the values of the variables aren't printed when you run the script (Figure 3.10), or they're not printed and you see many error messages, these are the aforementioned hiccups. Some of you may see nothing at all (a blank page). In any of these cases, continue reading the next three sections of the book for the appropriate solutions.
Figure 3.10 If the page doesn't display the submitted information, read the The Register Globals Problem section of the chapter.