Highlighting Fields That Require Attention


Rather than bombard the user with a list of warning messages, it's more user-friendly to highlight the fields in the form that require attention.

The technique to use here is very similar to the previous example, but rather than append each warning message to $err, you should give each field its own warning text. If you use an array of warnings, it's simple to see whether the form has been successfully validated by counting the elements in $warnings.

You write each rule to add an element to $warnings if validation of that field fails, as shown in the following example:

 if (!ereg("^[^@]+@([a-z\-]+\.)+[a-z]{2,4}$",         $_POST["email"]))   $warnings["email"] = "Invalid Format"; 

Then in the form itself, you can display this warning text next to the corresponding field:

 <TR>   <TD>Email Address</TD>   <TD><INPUT TYPE=TEXT SIZE=30 NAME="email"              VALUE="<?php echo $_POST["email"];?>"></TD>   <TD><b><?php echo $warnings["email"];?></b></TD> </TR> 

Listing 13.2 shows a revised register.php file that uses this technique to highlight missing or invalid field values.

Listing 13.2. Form Validation Using Inline Warnings
 <?php $required = array("name"  => "Your Name",                   "email" => "Email Address"); foreach($required as $field => $label) {   if (!$_POST[$field]) {     $warnings[$field] = "Required";   } } if ($_POST["email"] &&    !ereg("^[^@]+@([a-z\-]+\.)+[a-z]{2,4}$", $_POST["email"]))   $warnings["email"] = "Invalid email"; if ($_POST["telephone"] &&    !ereg("^\([[:digit:]]{3}\)[[:digit:]]{3}-[[:digit:]]{4}$",       $_POST["telephone"]))   $warnings["telephone"] = "Must be (555)555-5555"; if (count($warnings) > 0) { ?> <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>   <TD><?php echo $warnings["name"];?></TD> </TR> <TR>   <TD>Email Address</TD>   <TD><INPUT TYPE=TEXT SIZE=30 NAME="email"              VALUE="<?php echo $_POST["email"];?>"></TD>   <TD><?php echo $warnings["email"];?></TD> </TR> <TR>   <TD>Telephone</TD>   <TD><INPUT TYPE=TEXT SIZE=12 NAME="telephone"              VALUE="<?php echo $_POST["telephone"];?>"></TD>   <TD><?php echo $warnings["telephone"];?></TD> </TR> </TABLE> <INPUT TYPE=SUBMIT VALUE="Register"> </FORM> <?php } else {   echo "Thank you for registering"; } ?> 

The first loop assigns the warning text "Required" to any required field that is left blank. Each of the individual validation rules has its own warning text.

How you highlight a field that requires attention is up to your imagination and creativity with HTML. For instance, by checking for the presence of an element in $warnings for each field, you could change the style of the input box to a shaded background, like so:

 <INPUT TYPE=TEXT SIZE=30 NAME="email" <?php if ($warnings["email"]) echo "STYLE=\"shaded\"";?> VALUE="<?php echo $_POST["email"];?>"> 



    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