Hi guys,

OK, here is the issue I am having.
Perform a search and when you click the submit button, it takes you to the page that displays the info. Now, when the user clicks the back button on the browser it tries to re-submit the info. I don't want the info re-submitted, I just want the browser to take the user back to the relevant search page. Can I somehow trap the back button event and use that to redirect to a certain page?

Dani AI

Generated

— the browser behavior described is normal: a form submitted with POST leaves a POST entry in the session history, and navigating back to that entry can trigger a re-submit prompt. Trapping the back button is fragile and discouraged.

For search forms the simplest, most robust fix is to use GET so the query is part of the URL (bookmarkable, shareable, and never triggers the POST-resubmit dialog). When POST is required, implement Post/Redirect/Get (PRG): process the POST on the server, then immediately redirect to a results URL (GET). That makes the visible history entry a safe GET and prevents resubmission prompts. Minimal PHP PRG example:

<?php
// process.php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $q = trim($_POST['q'] ?? '');
  // validate/sanitize, build results or query params
  header('HTTP/1.1 303 See Other');
  header('Location: /search_results.php?q=' . urlencode($q));
  exit;
}
?>

Alternative approaches: server-side idempotency tokens or short-lived session keys (similar to ’s idea) can block duplicate processing for non-idempotent actions, but they add complexity. Avoid relying on beforeunload or trying to “catch” the back button (as noted) — those degrade UX and are inconsistent across browsers. Troubleshooting checklist: confirm the redirect is sent before any output, use a 303 after POST for explicit GET redirection, and test behavior across browsers.

Recommended Answers

All 2 Replies

While we don't trap the back button here on DaniWeb, we do trap leaving the page by any means possible (clicking a link, closing out the web browser, etc.) and we use jQuery to do so. Something similar to this ...

$(window).on('beforeunload',function() {  /* code here */  });

trapping the back button is not a good idea. I remember reading an article from the consortium entitled "Use standard redirects: Don't break the back button."

to prevent the form resubmission, we can place a session handler between the final form processor and the form page

form.php ----> sessionhandler.php (validates form and saves form data to session. If everything are valid, redirect user to the final processor) --> formprocessor.php ( process search requests. assign query and form submitted to session).

In the event, the user click on the back button.

formprocessor.php --> sessionhandler.php ( checks for the form submitted and the query. If these two are present, user redirected to the searchresult page. Else, the user is redirected to form page.)

forward flow:

form.php --> sessionhandler.php --> formprocessor.php

reverse flow:(pressing back button)

formprosessor.php -->(isset($_SESSION['submitted'] && isset($_SESSION['query']) ? 'redirect to result page' : 'redirect to form page';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.