Validating User Data


It's important to check the data that your user has entered by validating it and reporting any errors. To do that, we're going to develop some code over the next few chunks. In the previous chunk, we checked if there was some data waiting in a text field to determine whether we should display the welcome page, but sometimes that's not a good technique because you might not require data to be entered in that text field. Instead, developers often use a hidden field, and we'll name ours "seen_already". If this field is not present, the user hasn't seen the welcome page already, and we'll display it like this:

 if(isset($_REQUEST["seen_already"])){ . . . } else {     display_welcome(); } 

If the user has already seen the welcome page, then we should try to validate the data he or she sent us. We'll do that with a function named validate_data, which adds error messages to a global array named $errors. After the call to validate_data, you can check if there were any errors by checking the length of the $errors array with count($errors); if errors occurred, we should display the errors and the welcome page so that the user can re-enter his or her data. If no errors occurred, we can process the data that the user sent to us:

 $errors = array(); if(isset($_REQUEST["seen_already"])){     validate_data();     if(count($errors) != 0){         display_errors();         display_welcome();     }     else {         process_data();     } } else {     display_welcome(); } 

We'll implement the various functions used here on a case-by-case basis. The validate_data function will be different, for example, depending on whether you're checking for required data entry, whether a number is indeed an integer, and so on:

 function validate_data() {     .     .     . } 

The display_errors function will just display the error messages we've collected:

 function display_errors() {     global $errors;     foreach ($errors as $err){         echo $err, "<BR>";     } } 

The process_data function is also dependent on what kind of data you're working with; we'll see a variety of ways to implement this function in the coming chunks:

 function process_data() {     .     .     . } 

And we'll also implement the display_welcome function on a case-by-case basis. This function displays the controls the user should use. However, for all these cases, we want to make sure to create the hidden field "seen_already" here:

     function display_welcome()     {         echo "<FORM METHOD='POST' ACTION='phpvalidator.php'>";         .         .         .         echo "<INPUT TYPE='SUBMIT' VALUE='Submit'>";         echo "<INPUT TYPE='HIDDEN' NAME='seen_already'             VALUE='hidden_data'>";         echo "</FORM>";     } ?> </CENTER></BODY></HTML> 

OK, we've got our data-validating framework; now let's put it to work.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

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