Forum Moderators: coopster

Message Too Old, No Replies

Simple validation of form + change of value

         

wonderboy

6:21 pm on Mar 5, 2004 (gmt 0)

10+ Year Member



Hi,

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.

ergophobe

8:03 pm on Mar 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You just have to test all your conditions and then redirect

$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

webdevjim

8:31 pm on Mar 5, 2004 (gmt 0)

10+ Year Member



Hello,
I'd recommend doing it with javascript and then passing it to the php. Use the onSubmit event handler and then a js function. If you search for javascript, form validation, tutorial, you'll find plenty of code samples.

ergophobe

8:34 pm on Mar 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Javascript is probably better for an initial validation since you don't have to submit before validation.

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