Browser back button to search results
2 posts by 2 authors in: Forums > CMS Builder
Last Post: April 7, 2014 (RSS)
By gversion - April 6, 2014
Hello,
When I perform a search on my website and click on a search result listing if I then click the "back" button in my web browser, then the search results do not display.
I realise that this is a built in feature of the web browser but it seems to work fine if I search on this forum for a post, click the post from the search results and then click the "back" button in my web browser to return to the search results.
What's the secret to getting this to work correctly? Is it something to do with cookies or would it work if I used a GET form method rather than POST?
Any advice would be much appreciated.
Thank you,
Greg
By Chris - April 7, 2014
Hi Greg,
GET is a very simple solution. If you View Source in our forums, you can see that we're using method="GET" and a little trick with the submit button (type="button" onclick="this.form.submit();") so that it doesn't clutter up the URL with &submit=Search.
Another, more complex solution is to store the search criteria in a cookie (or several, or the session), then do a redirect to a regular GET page which reads from the cookie and performs the search (perhaps by injecting the criteria from the cookie into $_REQUEST). The page which resulted in the redirect gets skipped over when the user goes back in their browser. Note that you may need to account for accidentally remembering search criteria: when someone clicks to go to your list page, they're probably expecting to see all the results, not the filtered results from the last search they did. Getting this all working properly is non-trivial.
It's also possible to use Javascript to submit the form and replace the current page with the results returned, but then the search results page won't show up in the history at all, unless you manipulate the history with the HTML5 history API, but that's getting really, really complicated!
Hope this helps!
Chris