Displaying Validation Warnings


Another issue to consider is where to send the user when validation fails. So far we have assumed that a form submits to a processing script, and when one or more validation errors are found, the form prompts the user to use his or her browser's Back button to fix the errors.

Not only does this create one more step for the user to take in order to complete the formand in an online store, you want as few obstacles between a customer and a completed order as possibleit can also sometimes cause the data in the form fields to be lost when Back is clicked.

Whether the Back button causes data to be lost usually depends on the cache settings, either on the web server, in the user's browser, or at the user's Internet service provider. In many cases there is no problem. However, most notably when a PHP session has been started, no-cache headers are automatically sent to the browser, which causes data in form fields to be reset to their original values when you click the Back button. You will learn more about PHP sessions in Lesson 14, "Cookies and Sessions."

A good technique is to have the form and processing script in the same file and have the form submit to itself. This way, if there are errors, they can be displayed on the same page as the form itself, and the previously entered values can be automatically defaulted into the form.

Listing 13.1 shows a fairly complete example of a registration form, register.php. The name and email address fields are required, but the telephone number is optional.

Listing 13.1. A Sample Registration Form with Required Fields
 <?php $required = array("name"  => "Your Name",                   "email" => "Email Address"); foreach($required as $field => $label) {   if (!$_POST[$field]) {     $err .= "$label is a required field <br>";   } } if ($err) {   echo $err; ?> <FORM ACTION="register.php" METHOD=POST> <TABLE BORDER=0> <TR>   <TD>Your Name</TD>   <TD><INPUT TYPE=TEXT SIZE=30 NAME="name"              VALUE="<?php echo $_POST["name"];?>"></TD> </TR> <TR>   <TD>Email Address</TD>   <TD><INPUT TYPE=TEXT SIZE=30 NAME="email"              VALUE="<?php echo $_POST["email"];?>"></TD> </TR> <TR>   <TD>Telephone</TD>   <TD><INPUT TYPE=TEXT SIZE=12 NAME="telephone"              VALUE="<?php echo $_POST["telephone"];?>"></TD> </TR> </TABLE> <INPUT TYPE=SUBMIT VALUE="Register"> </FORM> <?php } else {   echo "Thank you for registering"; } ?> 

Note that the warning messages in this example appears even if the form has not yet been submitted. This could be improved by also checking for the existence of the $_POST array in the script by using is_array, but the check for $err would also need to look for $_POST; otherwise, the form would never be displayed.

The condition that checks $err spans the HTML form and, even though the PHP tags are closed around this chunk of HTML, the form is displayed only if that condition is true.

In this example, after the form has been completed successfully, only a simple message is displayed. This is where you would do any processing based on the submitted data, such as storing it to a database, which you will learn about in Lesson 19, "Using a MySQL Database." Alternatively, the script could force the browser to redirect the user to another page automatically by using a Location HTTP header, as follows:

 header("Location: newpage.php"); 



    Sams Teach Yourself PHP in 10 Minutes
    Sams Teach Yourself PHP in 10 Minutes
    ISBN: 0672327627
    EAN: 2147483647
    Year: 2005
    Pages: 151
    Authors: Chris Newman

    Similar book on Amazon

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