Forum Moderators: coopster

Message Too Old, No Replies

Preserve Form Data

Form submitted, contains errors, reload form with correct data intact

         

MatthewHSE

3:57 pm on Apr 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Most major sites, such as PayPal and Ebay, naturally do server-side error checking of form data. You know the concept; if you make a mistake, the form reloads with the correct data still intact and the errors marked.

So far, I haven't been able to find a free form processor with that kind of functionality. Many will do error checking, but it normally gives an error page with a "back" link. I'm looking for some way of doing the more user-friendly method of reloading the form, with the errors marked, and all the correct data still entered.

Unfortunately, while I can put together the error checking easily enough, I can't figure out a good way of keeping the form data in anything other than text fields. For instance, how do you get a checkbox or radio button to stay selected, and is there any way to make sure the right <option> in a select box stays selected?

I know there must be ways of doing this, but I sure don't know how. Any tips, references or tutorials will sure be helpful!

Thanks,

Matthew

<edit> Hey, this is my 1000'th post! </edit>

coopster

6:04 pm on Apr 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



A common practice is to check the variable upon entering your script to see if it already has a POSTed value and if not, assign it a default if necessary. Something along these lines ...
$lastname = (isset($_POST['lastname'])) ? $_POST['lastname'] : ''; 
// and in your form
<input type="text" name="lastname" value="<?php print $lastname; ?>" />

aeve

10:00 pm on Apr 13, 2005 (gmt 0)

10+ Year Member



This is something I've recently used to persist radio buttons on a self-submitting form. It defaults to the final one:

<div id="rbutt"><span>Organization type (choose one):</span><br />
<label for="ins">
<input type="radio" name="group" id="ins" value="Institution"
<?php if ($_POST['group'] == 'Institution') echo'checked="checked" '?>/>
$50 Institution</label><br />
<label for="gal">
<input type="radio" name="group" id="gal" value="Gallery"
<?php if ($_POST['group'] == 'Gallery') echo'checked="checked" ';?>/>
$30 Gallery</label><br />
<label for="art">
<input type="radio" name="group" id="art" value="Artist"
<?php if(($_POST['group']!= 'Gallery') && ($_POST['group']!= 'Institution')) echo'checked="checked" ';?>/>
$15 Artist</label><br />
</div>

tameone

3:10 am on Apr 14, 2005 (gmt 0)

10+ Year Member



Javascript and/or DOM are popular ways of doing error checking and if you want an alternative to those annoying pop-up boxes, there are ways to create the illusion that the page has reloaded.

Just depends on what kind of validation you need.

Here are a couple of sites with some reading on the subject.
[quirksmode.org...]
[xs4all.nl...]

I was just reading up on this a few weeks ago and the methods appear good. (I didn't get around to trying it myself.)

Good luck.

4string

4:03 am on Apr 14, 2005 (gmt 0)

10+ Year Member



Just search for Sticky Forms and you'll find a lot of tutorials.