Using GET or POST
The experienced HTML developer will notice that the feedback form is missing one thing: The initial <form> tag has no method value. This attribute tells the server how to transmit the data from the form to the handling script.
You have two choices with method: GET and POST. Many HTML coders may not be entirely clear on the distinction and when to use which. In truth, for the most part it doesn't make much difference (especially as you first begin using them), because either will generally get you the results you need.
The difference between using GET and POST is squarely in how the information is passed from the form to the processing script. The GET method sends all the gathered information along as part of the URL. The POST method transmits the information invisibly to the user. For example, upon submitting your form, if you use the GET method, the resulting URL will be something like this:
http://www.DMCinsights.com/phpvqs2/ → handle_form.php?title=Mr.&name= → Larry%20Ullman...
Whereas using the POST method, the end user will only see the following:
http://www.DMCinsights.com/phpvqs2/ → handle_form.php
When choosing which method to use, you may want to keep in mind these three factors:
- With the GET method, a limited amount of information can be passed.
- The GET method sends the input to the handling script publicly (which means, for example, that a password entered in a form can be viewed by anyone within eyesight of the Web browser, creating a larger security risk).
- A page generated by a form that used the GET method can be bookmarked, but one based on POST can't be.
Script 3.3. The method attribute with a value of post has been added to complete the form.
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>Feedback Form</title> 7 </head> 8 <body> 9 Please complete this form to submit your feedback: <br /> 10 11 <form action="handle_form.php" method="post"> 12 13 Mr. <input type="radio" name="title" value="Mr." /> 14 Mrs. <input type="radio" name="title" value="Mrs." /> 15 Ms. <input type="radio" name="title" value="Ms." /> 16 <br /> 17 Name: <input type="text" name="name" size="20" /> 18 <br /> 19 Email Address: <input type="text" name="email" size="20" /> 20 <br /> 21 Response: <select name="response"> 22 <option value="excellent">This is excellent.</option> 23 <option value="okay">This is okay.</option> 24 <option value="boring">This is boring.</option> 25 </select> 26 <br /> 27 Comments: <textarea name="comments" rows="3" cols="30"></textarea> 28 <br /> 29 <input type="submit" name="submit" value="Send My Feedback" /> 30 31 </form> 32 33 <!-- Script 3.3 - feedback.html --> 34 </body> 35 </html>
This book uses POST almost exclusively for handling forms, although you'll also see a useful technique where the GET method theory adds capabilities to your Web site (see "Manually Sending Data to a Page" at the end of this chapter). Either method will successfully pass along the data from the form, and the final decision about which method to use should be based on mitigating factors in your form and whether the resulting page should be bookmark-able. Experience will make the distinction clearer, but you can safely use POST most of the time.
To add a method to your script:
- Open feedback.html (Script 3.2) in your text editor.
- Within the initial <form> tag, add method="post" (Script 3.3, line 11). The form's method attribute tells the browser how to send the form data to the receiving script.
- Save the script and test again in your Web browser.
- View the source of the page to make sure all the required elements are present (Figure 3.7).
Figure 3.7 View the source of an HTML page to validate its contents.