Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Form Data Clears with Back Button

how to preserve data to allow user to correct error

2:32 pm on Apr 20, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 21, 2003
votes: 0

I have a standard contact form processed by a simple php script. If certain fields are left empty, the user gets an error message (which reloads into the open page) asking them to go back and fill in the emply fields. Problem is, any data they had already entered is cleared.

I've tried changing several browser security settings (on IE in particular) but nothing seems to work. I haven't been able to figure out if this is a browser issue or if there is a way to work around this with any coding changes. Sessions seem overkill for a simple contact form. Any ideas?

2:48 pm on Apr 20, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 12, 2002
votes: 0

You should validate the form with javascript onsubmit. If any values are incorrect, pop an alert message up, and return false on the submit to stop processing.
3:00 pm on Apr 20, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 0

This is because your browser or the server is clearing the cached form values.

While Javascript client-side validation saves the visitor time, you still have to validate it on the server side in case Javascript is disabled.

The best thing to do is have the "error" page be the form and populate the form with the submitted values. To do this you have a template of the form in your script. The perl-y equivalent would be:

print "content-type: text/html\n\n";
if ($error) {
print "you have the following errors in your form<br><ul>$error</ul>\n";
## $error is an <li>list of incomplete fields

print <<eoform;
<input type="text" name="fname" size="12" maxlength="30" value="$qs{'fname'}" />



8:38 pm on Apr 20, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:May 16, 2003
votes: 0

On the page with your form try sticking this php at the top:

header("Cache-control: private");


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members