Keeping Form Values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Keeping Form Values

Post by icesolid »

Scenario: A user is filling in a form and accidentally browses back.

Is there a way to use cache or something of the sort so that the user can just hit forward and the form values they were just filling out are not lost? Or not use cache at all and if the user hits the delete key or backspace key it will pull up a javascript prompt asking if they are sure they want to do that, if so what javascript command is used to detect this?
remyx187
Forum Newbie
Posts: 8
Joined: Tue Apr 29, 2008 11:49 am

Re: Keeping Form Values

Post by remyx187 »

ive had this problem before

It might go something like this:

Code: Select all

<form>
<input type="text" name="random" value="<?php echo $_POST['random']; ?>">
 
<select name="state">
  <option value="XX" <?php if($_POST['state'] == "XX") echo "selected"; ?>>XX</option>
</select>
</form>
 
etc.

Note: for the drop down list, you have to specify the if statement on each option value.

This might not be exactly what youre looking for but i think it should help a little.
Granted the user submits the form.
Last edited by remyx187 on Fri May 02, 2008 12:00 pm, edited 2 times in total.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Keeping Form Values

Post by aceconcepts »

but if they hit the back button without "posting" any values then the values will never exist!
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Keeping Form Values

Post by yacahuma »

The only thing that comes to mind is using javascript with onchange so that you DO keep the data in a cookie as soon as they type
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Re: Keeping Form Values

Post by icesolid »

Any other ideas?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Keeping Form Values

Post by aceconcepts »

You can't retrieve data that hasn't been posted.
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Re: Keeping Form Values

Post by Verminox »

Javascript? Cookie? Cache? Errm...
It's the default behaviour of a web browser

User types something in form...
User clicks back button by mistake...
OOPS! User clicks forward again...
The form values are restored

No scripting required.
Post Reply