Well,
Glad you don't like pop-up error messages, they're a pet peeve of mine too. Good thing PHP doesn't do silly stuff like that🙂 Seriously, you need client-side capabilities to do those pop-up windows and PHP is strictly server-side. Re-displaying the form and adding a red asterisk, or something equally as noticeable, is a good idea if fields weren't filled in. I'd re-display the entire form, filling the fields with the data the user provided in the previous attempt. You'd have to put all that info into hidden form fields to pass it to the next step anyways, so why not give them the option of editing it? When it comes to validation, remember that arrays are your friends!! Set your form up so that all the fields are array elements, then you just need to run the array through a loop or two to validate all the info you need. It's not simply restricted to checking for empty fields, you can build data-type checking, language filters, and all sorts of fun stuff in.... but let's keep it simple to start with. Try the following script:
<?php
function form($formFields) {
echo "<form action=\"some_page.php\" method =\"post\"><br>\n";
echo " <input type=\"text\" name=\"formFields[one]\" value=\"formFields[one]\"><br>\n";
echo " <input type=\"text\" name=\"formFields[two]\" value=\"formFields[two]\"><br>\n";
echo " <input type=\"text\" name=\"formFields[three]\" value=\"formFields[three]\"><br>\n";
echo " <input type=\"text\" name=\"formFields[four]\" value=\"formFields[four]\"><br>\n";
echo " <input type=\"text\" name=\"formFields[five]\" value=\"formFields[five]\"><br>\n";
echo " <input type=\"submit\" name=\"submit\" value=\"Submit this form\"><br>\n";
echo "</form><br>\n";
}
function validate($formFields) {
// loop through the passed array, making sure that all fields are filled
while (list($key,$var) = each($formFields)) {
// if the current field is empty
if ($var == "") {
// print an error message and re-display the form, filling it with existing data
echo "You have not filled in all the boxes. Try again.<br>";
form($formFields);
// tell the rest of the function not to execute and exit this loop
$continue = false;
break;
}
// if the current field isn't empty
else {
// add the field's data to a string
$str .= "Form field $key = $var<br>";
// allow the rest of the function to continue executing
$continue = true;
}
// if all fields were filled (ie. $continue is equal to boolean true)
if ($continue) {
// print the string created in the loop
echo $str;
}
}
// if the form has been submitted
if ($submit) {
// call the validation function, passing in the form data
validate($formFields);
}
// if the form hasn't been submitted, display the form
else {
form();
}
?>
This is obviously very simplistic and not a "real-world" example, but it should hopefully get you started in the right direction. Info on the list() and each() functions used in the while loop structure are found in the manual at:
http://www.php.net/manual/en/function.list.php
http://www.php.net/manual/en/function.each.php
respectively. HTH!!
Cheers,
Geoff A. Virgo