Hi,
The solution devin gave is prolly the quickest and best to go with.
Usually on longer forms I like to inform the user of ALL the fields that have mistakes in them. This is how I do it:
suppose I have the following fields/variables:
$firstname
$lastname
$email
$address
$city
$state
$zip
$phone
Here is the code that will print out all the invalid entries:
<?php
// create the empty array to start off with
$allErrors = Array();
if (!$firstname) {
$allErrors[] = "First name is required";
}
if (!$lastname) {
$allErrors[] = "Last name is required";
}
... you get the idea ...
// here is a sample for 5-digit zip codes.
// yeah, you can have 9, but let's limit it to 5 for the US :-)
if (!eregi("[0-9]{5}$", $zip)) {
$allErrors[] = "ZIP Code may only contain 5 digits";
}
// now we check to see if any errors occurred, and if so, print them out
if(sizeof($allErrors) > 0) {
// there are more than 0 errors
print "Please fix the following errors in your form:<br>\n";
// print all errors
foreach ($allErrors as $err) {
print " - " . $err . "<br>\n";
}
}
else {
print "no errors!";
}
Once again, the above is only really useful for longer forms so that PHP can tell the user ALL their errors instead of saying them one by one.
-sridhar