To check all the fields and alert the user of all the fields they left blank, create an array called $errors (or whatever you like). Everytime you discover that a field was blank (or contains info that isn't right), you push another error onto the $errors array like this:
<?php
$errors = array();
// x should not be blank
$x = $_REQUEST['x'];
if (!$x or $x=="") { $errors[] = "You left the X field blank"; }
// y should not be blank
$y = $_REQUEST['y'];
if (!$y or $y=="") { $errors[] = "You left the Y field blank"; }
// zip_code must be exactly 5 numbers
$y = $_REQUEST['zip_code'];
if (!ereg("^[0-9]{5}$",$y)) { $errors[] = "Zip Code must be exactly 5 numbers"; }
?>
Now that you have an array that contains all the errors, you can use an if statement that either (A) displays the errors to the user if there are errors or (😎 inserts them into a database like this:
if (count($errors)==0) { // Woo Hoo!!! There were no errors, insert into database
// Put your database insert statements here
}
else { // Darn! There were errors.
print "Sorry, there were some problems with the form. ";
print "Press your back button and make these changes:<br>";
foreach ($errors as $e) {
print "$e<br>";
}
}
Kudose will step in any moment now to tell me to use preg instead of ereg to validate the zip code. And he's right. I just don't remember the perl reg exp syntax variants off the top of my head so I'm leaving that as an exercise to the reader to improve my code. And yes, the empty() function is probably a better way to see if the field has no value. I'm a hack, what can I say.