Search Results and the Back Button

This is a general strategy question:

I have a simple search feature in PHP/mySQL that displays a list of organizations that are in a particular category, for example. Once the list is generated, you can click on an organization name to display details about the organization.

When I hit the back button, instead of going back to the search results, I get a “page expired” message. But then, if I refresh the page and hit the ‘resend’ button, the search results show up again.

How can I avoid the “page expired” notice and get right back to the search results directly?

Simply put, you cant. Sort of.

The “page expired” message you see is a browser-side safety against submitting POST data multiple times.

You can semi-circumvent this two ways: Using GET, or forcing a 302 from your form handler. Most likely, you’re more inclined to use GET.

This is because the your search form is submitted via POST. When you hit the back button from one of the detail pages, the browser recognises that you’ll have to resubmit the form data to regenerate the results page.

You can avoid this by submitting the form via GET instead. This also gives users the advantage of being able to bookmark specific searches.

Thanks. I’ll try that.

Well, I tried it and it worked like a charm - one more tidbit of knowledge to file away for the next time. Thanks for all your help!

As a rule of thumb, page requests that require a write operation (other than logging) should be done with POST, and page requests that require a read operation should be done as GET. Post is the default for forms because, usually, the data will be stored long term somewhere.

Another thing to keep in mind about Get requests - the pages they generate can be bookmarked by the user. With search results this is usually a desirable behavior.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.