|
Posted by Jerry Stuckle on 11/10/06 21:23
Jerim79 wrote:
> I am looking into form validation for my HTML form. I don't want to use
> javascript and was looking into using PHP. A crazy thought crossed my
> mind and I just wanted to get some input.
>
> What if I basically made the form one long if/then statement? I could
> ask for the first piece of information, test it against some
> conditions, then if it is okay I allow the user to fill in the next
> piece of information. I could go do down the page validation each box
> as we go. I could use hidden on all the text boxes until the previous
> condition had been validated. Some pseudocode:
>
> <?php
> <form action=POST method=registered.php>
> <input name"Fname" type="text">
> if ($Fname meets some condition)
> {
> <input name="Lname" type="text">
> if ($Lname meets some condition)
> {
> <input name="Number" type="text>
> if ($Number meets some condition)
> {
> }
> }
> }
> else;
> echo "Please enter a phone number";
> else;
> echo "Please enter a valid last name";
> else;
> echo "Please enter a valid first name";
> ?>
>
> I know the code isn't exact, I am just theorizing here. Could such a
> thing be done? Even remotely similar? I don't really plan on doing this
> anytime soon, but I was just curious. I only started PHP programming a
> few days ago so no need to point out how stupid this may sound.
>
Jerim,
Yes and no.
First of all, the PHP code will be executed before the page is ever sent
to the client. It's not interactive like javascript is.
So what you do is have the user complete the form and submit it. Your
code should then validate all fields. But you don't want to nest your
statements like that - for instance, what happens if they don't enter
either a first name or a phone number? They'll get a single message
which they correct - and they'll get a second message.
Rather, process each field individually, i.e.
$emsg = '';
if ($fname = '') // No first name
$emsg .= "Please enter a first name.<br>\n";
if ($lname = '') // No last name
$emsg .= "Please enter a last name.<br>\n";
if ($phone = '') // No phone number
$emsg .= "Please enter a phone number<br>\n";
....
if ($emsg != '')
echo $emsg;
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|