- 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
Manually Sending Data to a Page
The last example for this chapter is a slight tangent to the other topics but plays off the idea of handling form data with PHP. As you
learned in the section "Using GET or POST," if a form uses the GET method, the resulting URL is something like
http://www.DMCinsights.com/phpvqs2/ → handle_form.php?title=Mr.&name= → Larry%20Ullman...
The receiving page (here, handle_form.php) is sent a series of name=value pairs, each of which is separated by an ampersand (&). The whole sequence is preceded by a question mark (immediately after the handling script's name). In this example, handle_form.php has a $title variable with a value of Mr., a $name variable with a value of Larry Ullman (the %20 is a URL-encoded version of a space), and so forth.
Using this knowledge, you can send data to a PHP page without the use of the form. A simple script will easily demonstrate this concept, sometimes called the "GET method trick."
To create a simple greeting script:
- Create a new document within your text editor (Script 3.8):
<!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>Greetings!</title> </head> <body>
Script 3.8. This script will receive a variable and its value in the URL.
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>Greetings!</title> 7 </head> 8 <body> 9 <? php // Script 3.8 - hello.php 10 11 // Adjust display_errors and error_reporting, if desired. 12 ini_set ('display_errors', 1); 13 error_reporting (E_ALL & ~ E_NOTICE); 14 15 // This page should receive a name value in the URL. 16 17 // Say "Hello". 18 print "<p>Hello, <b>$name</b>!</p>"; 19 20 ?> 21 </body> 22 </html>
- Begin the PHP code:
<?php // Script3.8-hello.php
Address the error management, if desired:
ini_set ('display_errors',1); error_reporting (E_ALL & ~ E_NOTICE);
These two lines, which configure how PHP responds to errors, may or may not be necessary but can be helpful.
Print a message using an assumed $name variable:
print "<p>Hello, <b>$name</b>!</p>";
The $name variable is sent to the page through the URL. To access it, you can use $name if register_globals is enabled or {$_GET['name']} if it isn't. Again, you would use $_GET (as opposed to $_POST) because the value is coming from a GET method, and the curly braces help avoid parse errors.
- Complete the PHP code and the HTML page:
?> </body> </html>
- Save the script as hello.php and upload it to your Web server.
If you were to view this script now, it would only display Hello, !, because the $name variable has no value (Figure 3.17). To pass the script a $name value, use the GET method trick.
Figure 3.17 If the $name variable isn't assigned a value, the browser prints out this awkward message.
To manually send data to a PHP script:
- View the hello.php script in your Web browser by going to the appropriate URL, in this case, http://127.0.0.1/~larry/hello.php (Figure 3.17).
- Append to the URL the text ?name=Michael. You can (or, frankly, should) use your own name; just ensure that there are no spaces in your entry.
Press Enter (or Return) to reload the page in your browser with the new URL (Figure 3.18).
Figure 3.18 By setting the $name variable to Michael or some other name, you create a dynamic page that changes to address the specific user.
If you don't see the name printed in the browser, then you made a mistake. Check to be sure you included the question mark, which separates the file name from the data itself. Then be sure you used name, because that's what hello.php is expecting as a variable (remember that Name would constitute a different variable).
If desired, change the name value in step 2 and press Enter (or Return) to reload the page again (Figure 3.19).
Figure 3.19 Setting the value of $name to Jude has the same effect as changing the print() line to read print "<p>Hello, <b>Jude</b>!</p>";.
Each new name value creates a new dynamically generated greeting.