Forum Moderators: coopster
I have a page: index.php, included in this is form.php
I want to validate the form, so if any mistakes are made at all (e-mail, and presence checks) index.php reloads with fail.php included in place of form.php.
If everything is fine, it will run my submission code, and reload index.php with success.php in place of form.php.
Many thanks for your help on this matter, I know how to do this in principle, just the code that I find difficult to write :(
W.
$location = "success.php"; // default
// run tests
if (!email_is_valid($_POST['email'])) {
$location = "fail.php";
$error['email']=1; }
if (!phone_is_valid($_POST['phone'])) {
$location = "fail.php";
$error['phone'] = 1;
}
// redirect to location
header("Location: " . $location);
// on fail, use flags set to highlight input fields with problems.
$form = "<form yadda yadda>";
if (isset($error['email']) && $error['email']) {
unset($error['email'];
$mail_class = "error";
} else {
$mail_class = "";
}
$form.= '<p><span class="' . $mail_class . '">e-mail</span><input yadda yadda>';
Please note: the attributes "yadda yadda" will cause validation errors in some validators.
Tom
Still, you probably want your forms to work for users without JS, and therefore you need to allow submission and validation for them, and that means server-side validation as well as client-side.
Tom