Putting It All Together

   

Now that we have the building blocks to build smart, friendly, and easy form processing, let's take a look at a real-world example. This example, shown in Figure 4-1, provides a way for a user to register for a typical community site.

Figure 4-1. register.php

graphics/04fig01.jpg

Some of the fields are required and some are not. The script deems fields to be required if the field name ends with "_required".

The script also checks to make sure that a valid email address is entered and that the user enters two identical passwords.

Script 4-4 register.php

[View full width]

   1. <html>   2. <head>   3.   <title>Register</title>   4. <style type="text/css">   5.   .error {color:red; font-weight: bold;}   6. </style>   7. </head>   8. <body>   9. <?  10. $page = "register.php";  11.  12. function print_form() {  13.   global $page, $error, $print_again, $HTTP_POST_VARS;  14.   $fields = array("first" => "text",  15.            "last" => "text",  16.            "age" => "text",  17.            "email_required" => "text",  18.            "login_required" => "text",  19.            "password1_required" => "password",  20.            "password2_required" => "password");  21.   $labels = array("first" => "First Name",  22.            "last" => "Last Name",  23.            "age" => "Age",  24.            "email_required" => "Email",  25.            "login_required" => "Desired Username",  26.            "password1_required" => "Password",  27.            "password2_required" => "Confirm Password");  28.   ?>  29.   <form action="<? echo $page ?>" method="post">  30.   <?  31.   if($print_again) {  32.     ?><h3>You missed or incorrectly entered some fields. Please correct the <span  graphics/ccc.gifclass=error>red</span> fields. Passwords must match.<?  33.   } else {  34.     ?><h3>Please fill-in the following fields.</h3><?  35.   }  36.   ?>  37.   <table border="0">  38.   <?  39.   foreach($fields as $key => $value) {  40.     ?>  41.     <tr><td <? error_flag($error, $key); ?><?=$labels[$key]?>: </td>  42.       <td><input type="<?=$value?>" name="<?=$key?>" value="<? @print( graphics/ccc.gif$HTTP_POST_VARS[$key])?>"></td></tr>  43.     <?  44.   }  45.   ?>  46.   <tr><td colspan="2"><input type="submit" name="submit" value="Submit"></td></tr>  47.   </table>  48.   </form>  49.   <?  50. } // end function print_form  51.  52. function error_flag($error, $field) {  53.   if($error[$field]) {  54.     print("<td class=error>");  55.   } else {  56.     print("<td>");  57.   }  58. } //end function error_flag  59.  60. function check_form() {  61.   global $error, $print_again, $HTTP_POST_VARS;  62.   $print_again = false;  63.   //Check Required Fields Have Been Entered  64.   foreach($HTTP_POST_VARS as $key => $value) {  65.     if(($value == "") && eregi("_required$", $key)){  66.       $error[$key] = true;  67.       $print_again = true;  68.     } else {  69.       $error[$key] = false;  70.     }  71.   }  72.   //Verify Email  73.   if(!eregi("^[a-z0-9]+[a-z0-9_-]*(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)*\.( graphics/ccc.gif[a-z]+){2,}$", $HTTP_POST_VARS['email_required'])) {  74.       $error['email_required'] = true;  75.       $print_again = true;  76.       $HTTP_POST_VARS['email_required'] = "ENTER A VALID EMAIL";  77.   }  78.   //Verify Desired User Name Is Available  79.   $available = true;  80.   if(!$available) {  81.     $error['login_required'] = true;  82.     $print_again = true;  83.     $HTTP_POST_VARS['login_required'] = "Name Not Available: " .  graphics/ccc.gif$HTTP_POST_VARS['login_required'];  84.   }  85.   //Verify Passwords Match  86.   if($HTTP_POST_VARS['password1_required'] != $HTTP_POST_VARS['password2_required']) {  87.     $error['password1_required'] = true;  88.     $error['password2_required'] = true;  89.     $HTTP_POST_VARS['password1_required'] = NULL;  90.     $HTTP_POST_VARS['password2_required'] = NULL;  91.     $print_again = true;  92.   }  93.   //Print Again If Errors Are Found  94.   if($print_again) {  95.     print_form();  96.   } else {  97.     print("<h3>Thank you for completing the form!</h3>");  98.     // Do database insert, email, etc. Since Data Is OK  99.   } 100. } // end function check_form 101. 102. /***** MAIN *****/ 103. if(isset($submit)) { 104.   check_form(); 105. } else { 106.   print_form(); 107. } 108. ?> 109. </body> 110. </html> 

Script 4-4. register.php Line-by-Line Explanation

LINE

DESCRIPTION

1 8

Create the beginning of the HTML page.

10

Create a $page variable for use in the function to print the forms.

12

Create a print_form() function used to print the form to the screen.

14 20

Create an array to be used as input for the form-field names and types.

21 27

Create an array to be used as input for the form-field labels.

29

Start printing the form to the browser.

31

Check to see if the $print_again variable is set. If it is, then the user made a mistake entering information on his or her previous try filling in the form.

32

Print a message to the user notifying him or her that he or she made an error when filling out the form.

33

If there were no previous errors, then print out a simple message asking the user to fill out the form.

37

Begin printing the table for the form fields.

39

Loop though the $fields array and create the form. Notice how the keys for the $fields array are the same as the keys for the $labels array.

41

If there was an error, then highlight the label using the error_flag() function. Print the label for the field.

42

Print the field name and type (from the $fields array). If there was an existing value that the user entered, then print it out. The "@" sign suppresses any warnings that occur if the user is filling out the form for the first time or failed to enter a value previously. If you omit the "@" sign, then a warning is printed because the $HTTP_POST_VARS[$key] variable has not been declared.

46

Print the submit button for the form.

50

End the function.

52

Declare the error_flag function. The error_flag() function takes two arguments:

  • $error A boolean value of 0 or 1. 1 means an error occurred with this field. Either it was not filled in or the user's entry was invalid.

  • $field The key of the field in which the error occurred.

53

If the $error is 1 (true), then apply some formatting to the field label using CSS.

56

If there was no error, then just print a normal <td> tag.

58

End the function.

60

Declare the function check_form().

61

Allow the check_form() function to use and modify the variables listed.

64 71

These lines check to see if any fields ending with "_required" are not filled in. If they are not, then an error is generated for each field that is not filled in. Additionally, the $print_again variable is set to true.

73 77

These lines verify that the email address is the proper format. If it is not, then an error is generated for the email field and an error message is inserted into the form the next time it is printed.

79 84

These lines pseudo-check if a login name is available. You can change the setting on line 79 to false to see what happens when the name is not available. Normally, you would query your database or text-file list of users to verify whether a name was available or not. If the name is not available, then an error is generated for the login_required field.

86 92

These lines check if the password fields match. If they do not, then an error is generated and the existing passwords are set to NULL so that they are not reinserted into the form when it is printed again.

94

If the $print_again variable is set, then print the form again.

97

If the $print_again variable is not set, then all went well and you can insert the user registration into a database, or email the contents to an email address.

102

Begin the main program. If the submit button is pressed, then go to line 104. Otherwise, go to line 106.

104

Run check_form(), since we know the user has pressed the submit button.

106

The user has not yet filled out the form because the Submit button hasn't been pushed. Print the form using print_form().


   
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