Re: Re: form submission error trapping

From: Date: Mon, 25 Feb 2002 16:02:50 +0000
Subject: Re: Re: form submission error trapping
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-86104@lists.php.net to get a copy of this message
You do not need to use javascript. Simple create a form that submits to itself. And check the values, then reprint the form or redirect. Example: <?php if ($REQUEST_METHOD == "POST") { $required = array ("surname", "age"); $err_msg = ""; while (list ($key) = each ($required)) { if (!$HTTP_POST_VARS["$key"]) { $err_msg .= "Please enter a value for $key<br>\n"; } } if (!empty($err_msg)) print $err_msg; } ?> <form action="<?= $PHP_SELF ?>" method="POST"> <input type="text" name="surname" value="<?= $surname ?>"><br> <input type="text" name="age" value="<?= $age ?>"> <input type="submit" name="Submit"> </form> Hope that helps, -- Stewart On Mon, 18 Feb 2002, George Whiffen wrote: > Jason, > > You didn't mention Javascript checks. > > Personally I really dislike having to wati for a page to reload before finding > out that I've just failed to fill in a field. The Javascript to do basic > on-page checks is all pretty simple stuff. > > Of course this doesn't mean we can skip checking the data again in the php! > Javascript might be switched off, we might have a bug in the Javascript (easy to > do), or we (or someone else) might want to simulate form entry via a url link. > It's also quite likely that there may be checks e.g. checks for duplicate > entries, which cannot be done on a form. > > Here's some skeleton code that handles a basic form with both php and javascript > checks, (I've deliberately tried to keep the php/Javascript as similar as > possible). You can try it at http://www.whiffen.net/simple.php > if you want. > > *** simple.php*** > <SCRIPT LANGUAGE="php"> > > if (isset($surname)) > { > $errormessage = ""; > > if ($surname == "") > { > $errormessage = $errormessage . "<br>You must enter a value for surname"; > } > > if ($age <= 0 or $age != floor($age)) > { > $errormessage = $errormessage . "<br>Age must be a whole number"; > } > > if ($errormessage == "") > { > // do whatever you have to with the data and maybe finish with > // a redirect to a success page > } > } > > print ' > <HTML> > <HEAD> > <SCRIPT LANGUAGE="javascript"> > function check() > { > var message = ""; > if (document.myform.surname.value == "") > { > message = message+"\nYou must enter a value for surname"; > } > if (document.myform.age.value != parseInt(document.myform.age.value)) > { > message = message+"\nAge must be a whole number"; > } > if (message != "") > { > alert("You have the following errors to correct:"+message); > return false; > } else { > return true; > } > } > </SCRIPT> > </HEAD> > <BODY> > <H1>MY FORM</H1> > <FORM name=myform method=post onSubmit="return check();"> > '; > if ($errormessage != "") > { > print 'You have the following errors to > correct'.$errormessage.'<BR>'; > } > print ' > Surname: > <INPUT TYPE=TEXT NAME="surname" VALUE="'.$surname.'"> > <BR> > Age: > <INPUT TYPE=TEXT NAME="age" VALUE="'.$age.'"> > <BR> > <INPUT TYPE=SUBMIT> > <BR> > <A HREF="'.$SCRIPT_NAME.'s">Source</A> > </FORM> > </BODY> > </HTML> > '; > </SCRIPT> > > You'll see that > http://www.whiffen.net/simple.php?surname=whiffen&age=23.2 works > perfectly well too. > > Personally, I'm not a huge fan of Javascript. It always seems to be much more > trouble than php for some reason, but it has its place, (as long as you never > rely on it!). > > George > > Jason Dulberg wrote: > > > I am working on some error trapping for several forms on my site. After > > visiting a bunch of websites, I've noticed 2 common methods of displaying > > error messages. > > > > 1. display an error box on a new page and force the user to hit the <back> > > button > > > > 2. display the form again with appropriate error text and pre-filled fields. > > > > I have part of the error on the new page working but I'm running into the > > infamous no contents in the form after going <back>. > > > > There are some useability issues with forcing the user to hit the back > > button -- some just don't want to bother. > > > > Is there a way to display the form w/original contents and error messages > > 'without' having to code the entire form twice? I have about 5 forms with 50 > > fields or so each. > > > > What would be the best way to go about redrawing the form with the errors > > shown beside each field? > > > > Any suggestions are greatly appreciated. > > > > __________________ > > Jason Dulberg > > Extreme MTB > > http://extreme.nas.net > > > > >

« previous php.general (#86104) next »