Post and Get

   

There are two methods that you can use when creating a form in HTML. They are post and get, as in:

 <form action=submit.php method=post>  

or:

 <form action=submit.php method=get>  

If you don't specify a method, then the Web server assumes that you are using the get method. So what's the deal? They do the same thing right? Well, almost. You may have noticed that the URL looks a lot longer after you submit a form that uses the get method. For example, you may see something like:

 http://web.com/form.php?name=fred&age=20&comments=This+site+rocks 

That's because the get method puts the contents of the form right in the URL. There are a few disadvantages to this. First, depending on your Web server's operating system and software, there is a limit to how much data you can send through a form using the get method. On many systems, this limit is 256 characters. Also, the individual get queries may be stored in your Web server logs. If you are using space on a shared server, then other people may be able to decipher data sent from your forms that use the get method.

The post method was created to correct the inadequacies of the get method. The information sent using the post method is not visible in the URL, and form data cannot be deciphered by looking in the Web server logs. There also isn't such a small limit on the amount of data that can be sent from a form. Again, it depends on your server, but you probably won't ever hit the limit of sending data using the post method for a text-based form.

I use the post method for my scripts unless I need to debug something. When you need to debug something on a form, it is easy enough to switch to the get method (by changing the action line in your script) and then check the URL after you submit your buggy form. You can usually pick up typos and such with a quick look.

Making Forms Friendly

The first script in this chapter, form1.php, shows you how you can make a simple script to determine if the user filled in all of the fields. If the user fails to fill in all the fields, then the script prints out the form again, printing out the field names in red for the fields that the user missed. It also saves any text that the user previously entered and automatically populates the form fields with those values.

The script contains three functions:

  • print_form()

  • check_form()

  • error_flag()

The complete script and explanation follows:

Script 4-1 form1.php

[View full width]

  1.  <?  2.  $page = "form1.php";  3.  ?>  4.  <html>  5.  <head>  6.  <style type="text/css">  7.    .error {color:red;}  8.  </style>  9.  </head> 10.  <body bgcolor="#FFFFFF" text="#000000"> 11.  <? 12.  function error_flag($error, $field) { 13.    if($error[$field]) { 14.      print("<td class=error>"); 15.    } else { 16.      print("<td>"); 17.    } 18.  } //end function error_flag 19. 20.  function print_form() { 21.    global $error, $print_again, $first, $last, $page; 22.    ?> 23.    <form action="<? echo $page ?>" method="post"> 24.    <? 25.    if($print_again) { 26.      ?><h3>You missed some fields. Please correct the <span class=error>red</span>  graphics/ccc.giffields.<? 27.    } else { 28.      ?><h3>Please fill-in the following fields.</h3><? 29.    } 30.    ?> 31.    <table border="0"> 32.      <tr><td <? error_flag($error, "first"); ?>First Name:</td> 33.        <td><input type="text" name="first" value="<?=$first ?>"></td></tr> 34.      <tr><td <? error_flag($error, "last"); ?>Last Name:</td> 35.        <td><input type="text" name="last" value="<?=$last ?>"></td></tr> 36.      <tr><td colspan="2" align="center"> 37.        <input type="submit" name="submit" value="Submit Form"></td></tr> 38.    </table> 39.    </form> 40.    <? 41.  } // end function print_form 42. 43. function check_form() { 44.   global $error, $print_again, $first, $last; 45.   $error['first'] = false; 46.   $error['last'] = false; 47.   $print_again = false; 48.   if($first == "") { 49.     $error['first'] = true; 50.     $print_again = true; 51.   } 52.   if($last == "") { 53.     $error['last'] = true; 54.     $print_again = true; 55.   } 56.   if($print_again) { 57.     print_form(); 58.   } else { 59.     print("<h3>Thank you for completing the form!</h3>"); 60.   } 61. } // end function check_form 62. 63. /***** MAIN *****/ 64. if(isset($submit)) { 65.   check_form(); 66. } else { 67.   print_form(); 68. } 69. ?> 70. </body> 71. </html> 

Script 4-1. form1.php Line-by-Line Explanation

LINE

DESCRIPTION

2

Set the $page variable to the script name.

12

Define the error_flag() function. It takes two arguments, $error and $field. The $error variable is actually an array, which contains all of the errors that were found in the form. The $field variable is the current field for which the script is generating an error.

13 17

Check to see if there is an error for this particular field. If there is an error, then the script prints <td class=error>, which causes that field name to be printed in red. Otherwise, the script prints out a normal <td> tag.

18

End the error_flag function.

20

Declare the print_form() function.

21

Define some global variables so that they can be easily used in the function. The variables are:

  • $error An array of errors (if any) returned from the check_form() function.

  • $print_again A boolean variable returned from the check_from() function.

  • $first and $last Variables returned from the forms in this field.

  • $page We use this variable in the form action=… line. Useful so that we can easily change the name of the page and not worry about breaking our scripts.

All of these variables are empty if this is the first time that this script is being run.

22 24

Print out the form tag. Note that we include the $page variable. Also note the post method.

25 30

The if/then/else statement checks to see if the $print_again variable is set to true. If it is, then we print a message saying that some of the fields were missed previously. If the $print_again variable is not set, then we just print a message asking the user to fill in the form.

31 40

Print out the form fields. This is a pretty standard form except for two things:

  1. Each field label is preceded by the function error_flag instead of a normal <td> tag. This is because we want to check to see if the users previously screwed up entering information into this field. If they did (and they do at one time or another), then the error_flag() function prints out <td class=error> instead of <td>. The error Cascading Style Sheets (CSS) class simply makes the text in that cell red.

  2. Each text input has a value associated with it for example, value="<?=$first ?>". If this is the first time that the user is filling in the form, then these values are blank. However, if the user previously made an error and this is the second or subsequent time that he or she is filling out the form, then the previously entered value is shown in the form. That way, the user doesn't have to re-enter information into the form.

41

End the print_form() function.

43

Define the check_form() function.

44

Allow the function to use the global variables listed. Note that these are the same variables used in the print_form() function.

47

Initialize the $print_again variable to false. Assuming all goes well with the data that the user entered, then this should remain false.

48 51

Check the $first variable (representing the user's first name) to see if the user entered a value. If he or she did not enter anything into this field, then we generate an error by setting the first element in the $error array to true and also setting the $print_again variable to true.

52 55

These three lines do the same thing as lines 48 51, except that this time we are checking the $last variable (representing the user's last name).

56 60

If an error was encountered and the $print_again variable is set to 1 (true), then print the form again, else print a message to the user.

61

End the check_form() function.

64 65

If the Submit button was pressed, then execute the check_form() function.

66 68

If the Submit button was not pressed, then execute the print_form() function.

70 71

Close out the HTML page.


   
Top


Advanced PHP for Web Professionals
Advanced PHP for Web Professionals
ISBN: 0130085391
EAN: 2147483647
Year: 2005
Pages: 92

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net