Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can web form content be preserved for the back button

When a web form is submitted and takes the user to another page, it is quite often the case that the user will click the Back button in order to submit the form again (the form is an advanced search in my case.)

How can I reliably preserve the form options selected by the user when they click Back (so they don't have to start from scratch with filling the form in again if they are only changing one of many form elements?)

Do I have to go down the route of storing the form options in session data (cookies or server-side) or is there a way to get the browser to handle this for me?

(Environment is PHP/JavaScript - and the site must work on IE6+ and Firefox2+)

like image 209
Peter Howe Avatar asked Oct 29 '08 16:10

Peter Howe


People also ask

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.

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.


2 Answers

I believe you're at the mercy of the browser. When you hit back, the browser does not make a new request to the server for the content, it uses the cache (in nearly every browser I've seen anyway). So anything server-side is out.

I'm wondering if you could do something very complicated like storing the search result in a cookie during the onunload event of the results page, and then reading the cookie in javascript on the search page and filling in the form - but this is just speculation, I don't know if it would work.

like image 157
Tom Ritter Avatar answered Oct 12 '22 23:10

Tom Ritter


I'd put it in the session.
It's going to be the most reliable and even if they don't go straight "back", it'll still have their search options there. Putting it in the cookie would also work, but wouldn't be recommended unless it's a very small form.

like image 40
Greg Avatar answered Oct 12 '22 23:10

Greg