Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing form data when clicking the back button on browser [duplicate]

Tags:

forms

php

When I click back on any browser I lose all the data which was inputted in the form on both drops downs and text input types.

Is this a server , Browser or a coding issue ?

Thanks

like image 754
Oliver Bayes-Shelton Avatar asked Sep 28 '09 06:09

Oliver Bayes-Shelton


People also ask

What happens when browser Back button is pressed?

For pages that are set as non-cached, the browser reloads the page from the server when you press Back, as though it was the first time you are visiting it. For cached pages, the browser displays it out of the cache, which is much faster.

How do I keep form even after page is redirected back?

It doesn't matter if you redirect to another page or the same one. You just need to check whether a value for particular field is in post data and, if yes, render it in the value attribute.

How do you stop re submitting a form after clicking back button?

You can check if the user clicked the back button, disable form if true. Another way is by storing a cookie which you check on page load, if it exists you can disable the form.


2 Answers

Its a browser issue. Browser behave differently when the back button is clicked -- this behavior mostly depends on user privacy settings. In addition, most browsers will automatically reset form upon hitting back button for pages viewed over HTTPS connection regardless of their normal behavior for HTTP connections. In addition, many HTML forms that rely heavily on AJAX do not properly -- or consistently -- restore their previous state.

Edit ----

Now that you mention using PHP and MySQL, assuming that you are also using sessions... the browser will not pre-populate the form when you hit the back button. PHP-session powered pages are not cached in browsers and hitting back button to go back to previous page is just like opening that page again from the very beginning -- most browsers will not bother restoring the form values.

like image 156
Salman A Avatar answered Oct 06 '22 11:10

Salman A


This applies to PHP and IE8.

Not only must you set cacheing to private, but you must remove the 4 cacheing headers and this can only be done with PHP 5.3.

In PHP 5.2 you can only set the 4 headers to blank values if using the Zend Framework's setHeader() method. For some reason is not sufficient on IE8 to set the 4 header values to empty values. Here's the code for PHP 5.3:

header_remove("Expires"); 
header_remove("Cache-Control"); 
header_remove("Pragma"); 
header_remove("Last-Modified"); 
like image 35
Chung Lau Avatar answered Oct 06 '22 10:10

Chung Lau