0

Possible Duplicate:
I am confused about PHP Post/Redirect/Get

I have a form on a page, so I get the "this page has used data, are you sure you want to refresh" message when I reload the page.

I looked into the HTTP-303 response code, which gets rid of the message (I'm on Chrome btw) until I submit the form once, and then it starts bugging me again.

Basically, I have an optional "contact me" form, that shouldn't throw a prompt if the page is refreshed. I don't think/see why I should need a separate page for submission for a short form like this. Can anyone help me out?

4

3 に答える 3

1

最善のオプションは、Ajaxを使用することです。そうすれば、リロードする必要はありません。私は通常、JQueryで使用します。
したがって、次のような結果になります。

function send_message(vMessage,vWho) {
    $.ajax({
        url:"messageSender.php",
        data:{message:vMessage,from:vWho},
        type:"POST",
        success:function(result) {
            // DO SOMETHING HERE, LIKE AN ALERT
        }
    });
}
于 2012-12-05T01:12:17.440 に答える
1

Final Solution: How to prevent form resubmission, on the same page

  1. You should have the action="something" tag. Required in HTML5
  2. Check if a form was submitted. If you don't, and have a header('Location: ...') it will be like an infinite loop (crap out). So something like if (isset($_POST['some_submit_btn'])) {
  3. Do you processing logic
  4. Do header('Location: same_page') (eg: header('Location: ' . $_SERVER['PHP_SELF'])) and exit

Your page can now refresh regularly, even after submitting a form. The redirect clears the POST request or whatever.

Others have pointed out Ajax is a simpler solution. I've accepted it as an answer, but in case anyone wants to do it this way I put this for reference.

Note: If you want to pass data between the two pages/refreshes, you have to pass it through sessions or cookies (I prefer sessions). I don't think there's a "clean" workaround

于 2012-12-05T04:23:18.627 に答える
0

You need to redirect after you saved the form data. It can be done on the same page, it's not a problem, you just need to issue a GET request instead of a POST.

于 2012-12-05T00:02:36.273 に答える