当刷新php页面时,浏览器弹出一条消息给用户[复制]

当刷新php页面时,浏览器弹出一条消息给用户[复制]

问题描述:

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.

可能重复: strong>
避免在php页面上重发表单 p> blockquote>

Index.html strong> p>

 &lt; form method =“post”action =“demo.php”&gt; 
  
&lt; input type =“text”name =“fname”/&gt;  
&lt; input type =“text”name =“lname”/&gt; 
&lt; / form&gt; 
  code>  pre> 
 
 

demo.php strong> p>

 &lt;?php 
 $ firstname = $ _POST ['fname']; 
 $ lastname = $ _POST ['lname']; 
 // 一些更多的PHP代码来填充网页
?&gt; 
  code>  pre> 
 
 

所以用户输入他的名字和姓氏并提交表格,然后demo.php 它的工作和工作正常,但当我按下F5或刷新demo.php时,我会从浏览器弹出 p>

  //此消息来自谷歌浏览器 
您正在查看的页面使用了您输入的信息。
返回该页面可能会导致重复任何操作。
是否要继续?
 
 //此消息来自IE 7或 8 
要再次显示网页,Internet Explorer需要重新发送您之前提交的信息。 如果您正在购买,
 
您应该单击取消以避免重复交易,否则请单击重试。
  code>  pre> 
 
 

为什么我会这样做? 我想刷新页面。 我不希望从浏览器中弹出该消息。 只需根据以前提交的值刷新页面,因为它在我的数据库中创建重复值。 p> div>

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.

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.