0

In my web application, I have a form which allows searching for items in the database. When a user clicks a search result, it opens that result in the same window.

But, then a user hits the back browser to go back to his search results he finds an empty search form (off course, because that page is reloaded).

What is the best way to "remember" the search form values when hitting the "back button"? I found some different ways: - history.js (store variables after # in the URL) - store it in database - store it in $_SESSION date - store in in a cookie

When the user visits the URL of the search page in the normal way (not going "back"), it should just be empty.

Please advice. Thank you.

Mbrouwer88
  • 2,182
  • 4
  • 25
  • 38
  • 1
    if you know the options, pick one. "beast" is just an opinion –  Nov 28 '16 at 22:40
  • 2
    why would reloading the page lose the search results? You make it sound like this is something obvious but I assure you it isn't. If I had to guess, you are loading the search results via ajax and not updating the url hash or history with a unique url so the history/search is not preserved. Which IMO is a poor design "off course". – Jonathan Kuhn Nov 28 '16 at 22:41
  • Store however you please. My suggestion would be to adding a HTTP var to the endpoint and parse the $_SERVER['HTTP_REFERER'] looking for that var. If it's set, you know what to do. – Daerik Nov 28 '16 at 22:42
  • 1
    If your search form can use `GET` instead of `POST`, the search term will be in the URL, and thus available when a user goes back. – fredrover Nov 28 '16 at 22:42
  • @JonathanKuhn, you are right. I use the data in the form to perform an Ajax call to load the results in server-side rendered datatables. I do not want to refresh the whole page when "hitting the search button". Do you have an example on how to "update the URL hash or history" in this situation? – Mbrouwer88 Nov 28 '16 at 22:45
  • I would suggest using something like history.js like you mentioned. That will update the url without reloading the page. Then you just need to make it meaningful enough that you can recreate the page and search results. – Jonathan Kuhn Nov 28 '16 at 22:52

0 Answers0