Topic awaiting preservation: php form validation? |
|
---|---|
Author | Thread |
Bipolar (III) Inmate From: Norway |
posted 07-17-2005 21:37
Hi! code: <?php if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $epost)) { echo "<p>Ugyldig e-post adresse!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($navn == "") { echo "<p>Fyll ut navn!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($adresse == "") { echo "<p>Fyll ut Adresse!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($postnr == "") { echo "<p>Fyll ut postnummer!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($sted == "") { echo "<p>Fyll ut sted!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($epost == "") { echo "<p>Fyll ut e-post!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($tlf == "") { echo "<p>Fyll ut telefon nummer!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($tlf == "") { echo "<p>Fyll ut telefon nummer!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif (mail($to, $subject, $message, $headers)) { echo "<p>Takk $navn, skjemaet er sent! Vi tar kontakt så fort som mulig!</p>"; } else { echo "<p>Skjemaet ble ikke sent!</p>"; } ?>
|
Paranoid (IV) Inmate From: France |
posted 07-17-2005 22:24
The PHP code is executed once the the page processing the form is loaded, that is when the FORM is submitted, so it's normal that the FORM is submit even though the fields do not match your conditions. code: <form onsubmit="return checkForm( this );"> <!-- your fields --> </form> <script type="text/javascript"> function checkForm( theForm ) { errorArray = [] if( !theForm.elements['epost'].value.match( /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/ ) ) errorArray[ errorArray.length ] = "email address invalid" if( theForm.elements['tlf'].value=="" ) errorArray[ errorArray.length ] = "telephone number invalid" if( !errorArray.length ) return true alert( "** Error **\n\n\t"+ errorArray.join( "\n\t" ) ) return false } </script> |
Paranoid (IV) Inmate From: Florida |
posted 07-17-2005 22:30
Better to have PHP spit out a "you're a dork, fill in all the fields" page, imo. JavaScript for forms...eww. |
Paranoid (IV) Inmate From: France |
posted 07-17-2005 22:58 |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 07-18-2005 02:23
I usually do both I just think it's a better user experience if you check with JS particularly if there are password fields involved. (I'll always forget one thing, get the next page and then forget to put the passwords again) |
Bipolar (III) Inmate From: Norway |
posted 07-18-2005 23:14
I'll use JavaScript as well then. Thanks! |
Maniac (V) Mad Scientist From: Rochester, New York, USA |
posted 07-19-2005 18:27
I will agree, do both. Javascript can make the experience for the user a whole lot easier, but you have to use the server to validate as well or else you will end up having problems that are far more malicious. |
Maniac (V) Inmate From: under the bed |
posted 07-19-2005 18:53
Can anyone explain how/why using javascript makes a better user experience? |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 07-19-2005 19:43
It really tends to depend on the form and the amount of validation needed. For a basic form with a couple of inputs and a textarea or two it's no big deal. |
Maniac (V) Mad Scientist From: Rochester, New York, USA |
posted 07-19-2005 21:03
You can use your onchange to fire your validation, and then you can have the same type of warning highlight showup on your forms as the user is filling them out. |