Håkon Hægland
Håkon Hægland

Reputation: 40758

Prevent browser form-resubmission alert

How can I avoid the the browser form-resubmission alert?

enter image description here

This question seems to have been discussed a lot here on SO, for example:

What I do not get from the previous discussion, is how I can use the posted data and incorporate it into the html. The previous links discuss how to use the php header function to send a get request to itself. But when sending this get request, the posted data will no longer be available to the new page, (since we cannot use the php post method..)

I would like to do this without using the php or javascript session storage technique (or saving the posted data to a temporary mySQL database).

For a simple example:

<html>
   <body>
      <form action="post.php" method="post">
      User name: <input type="text" name="user"><br>
         <input type="submit" value="Submit">
      </form>
   </body>
</html>

where post.php is:

<html>
   <body>
      <?php
          echo "<p>".$_POST['user']."</p>";
      ?>
   </body>
</html>

Pressing CTRL-R in google chrome on the second page brings up the alert.

Upvotes: 2

Views: 2543

Answers (3)

Jayesh Naghera
Jayesh Naghera

Reputation: 95

Use this:

<script>
if(window.history.replaceState) 
{
window.history.replaceState(null,null,window.location.href);
}
</script>

Upvotes: 1

Eugen Konkov
Eugen Konkov

Reputation: 25143

you may rewrite the browser history object

history.replaceState("", "", "/the/result/page");

See this

Upvotes: 0

Headshota
Headshota

Reputation: 21449

Do a redirect from post.php. Save data in session or in database and retrieve from redirect page.

Example Scenario:

  • Submit the form
  • Save the user record to db, get the id of the new record e.g. in $id
  • redirect using header, something like:
    header('Location: result.php?user_id='.$id);
  • get the user record from db, with the provided id and show it to the user.

Upvotes: 3

Related Questions