Back button/clearing form

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
btp_admin
Forum Newbie
Posts: 1
Joined: Sun May 14, 2006 4:10 pm

Back button/clearing form

Post by btp_admin »

Anyone know of any php that would stop a form from being cleared everytime somebody presses the back button?

Thanks.

Chris
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

You are going to want to store the form information in a session variable. and then echo the variable into the value of the form.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think the cache control setting of the header() function might help.

There is a user supplied comment on that page that might be useful to you as well...
hervard at gmail dot com wrote:When using HTML forms, using the browser's back button will sometimes display a message regarding using cached data, and will ask you to refresh the page. This can be very disconcerting for some users, as they might not know whether to hit Refresh or not.

To force pages to always load the data that was entered in the form prior to hitting a submit button, and prevent the browser's cache message from displaying, use the following code:

Code: Select all

<?php

   // Original code found at http://www.mnot.net/cache_docs/

   header("Cache-Control: must-revalidate");
   $offset = 60 * 60 * 24 * -1;
   $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
   header($ExpStr);

?>
This will tell the browser that the page will expire in one day, and the cached form data will be used without prompting the user at all.

I have tested this in Internet Explorer 6, Firefox 1.5, and Opera 8.51, and it works as intended. I have tried other cache-control and expiry variants, but they either do not work, or do not work in every browser. This code appears to be a winner.
Post Reply