Preferably through PHP, I want to know if the user used the back button to get to a page so that I can save the state of the page that they were on when they left. It is a search page that uses AJAX, so there is nothing in the URL that would allow me to refill the inputs on the page to recreate the form that was previously submitted. Is there a way to use session data and knowledge of if the user pushed the browser back button to fill the input fields that the user filled in?
Asked
Active
Viewed 134 times
1
-
1You can't definitively know if they used the back button, but you can certainly save state with JavaScript. You might also consider saving it in local storage. – Brad Apr 19 '14 at 04:14
-
1[this](http://demos.jquerymobile.com/1.1.1/docs/pages/page-cache.html) may be relevant to you. – 13ruce1337 Apr 19 '14 at 04:16
-
1I don't have experience setting this up in PHP, but many servers will detect when the user has visited the site and respond with a `304 not modified` response code. You can certainly hook that on the server side, and you can detect it when the [page has been requested from javascript](http://stackoverflow.com/questions/8571227/) but I'm less certain if you can detect the status of the *previous* page load that served up the page executing said javascript. – Patrick M Apr 19 '14 at 04:24
-
1On the client side, your question is very similar to one I answered here: http://stackoverflow.com/questions/23164888/distinguish-f5-or-open-for-browser/23165275#23165275 – Jeremy J Starcher Apr 19 '14 at 04:29
1 Answers
1
Yes, but I only know of how to do it in JavaScript. But if you're using AJAX, then you should be able to use this.
When you load your AJAX page, do this:
history.pushState(postData, "Search", pageURL);
Where above, the postData will be your form information you need, preferably as JSON.
Then this event will handle back button events
window.onpopstate =function(event) {
console.log(event); // log the event to the console to see what data you'll need
}
I use this on my personal website and it works fine for me.

Bryan Way
- 1,903
- 3
- 17
- 27