0

Possible Duplicate:
Avoid resending forms on php pages

Index.html

 <form method="post" action="demo.php">

             <input type="text" name="fname"/> 
             <input type="text" name="lname"/>
 </form>

demo.php

 <?php
       $firstname = $_POST['fname'];
       $lastname = $_POST['lname'];
       //some more php code to fill the webpage
  ?>

So the user enter his first name and last name and submits the form and then demo.php does its job and its working fine, but when I press F5 or refresh demo.php the next time I get this pop up from the browser

     // this message is from google chrome
     the page that you're looking used information that you entered.
     Returning to that page might cause any action to be repeated.
     Do you want to continute?

    // this message is from IE 7 or 8
    To display the webpage again,Internet Explorer needs to resend information
    you've previously submitted. If you were making a purchase, 
    you should click cancel to avoid duplicate transaction,else click retry.

Why do I get this? I want to just refresh the page. I don't want that message to be popped out from the browser. Just refresh the page according to previously submitted values because its creating duplicate values in my database.

Community
  • 1
  • 1
niko
  • 9,285
  • 27
  • 84
  • 131

3 Answers3

2

You need to respond with an HTTP redirect, the recommended flow is to process the POSTed data then redirect to another page with an internal identifier of the data processed.

If you refresh a webpage that has just return from a POST, then the expected behavior is to make another POST with the same values as the last time.

The header function can be used for that purpose in php http://php.net/manual/en/function.header.php

To keep the state in the redirection you can use php $_SESSION or passed the data in the query string of the redirected URL.

cyraxjoe
  • 5,661
  • 3
  • 28
  • 42
0

Read on Post/Redirect/Get design pattern: http://bakery.cakephp.org/articles/luciansabo/2011/08/12/post_redirect_get_design_pattern_component

Roman Newaza
  • 11,405
  • 11
  • 58
  • 89
0

You get this message, because refreshing a page sends same request, as it did when the page was previously loaded. In your case, it was a submit of the form. Browser warns you, that if you refresh the page, POST data will be sent again and if script is not protected, it might create duplicate values in database or something similar.

One way to solve it is to redirect user to the same page (POST data is always lost upon redirect). If you want to be safe even if user presses "back" after redirect and resubmits the data, here's the solution.

Community
  • 1
  • 1
Ranty
  • 3,333
  • 3
  • 22
  • 24