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?

Community
  • 1
  • 1
Raekye
  • 5,081
  • 8
  • 49
  • 74
  • 2
    There are many posts about post/redirect/get that you may want to read. Here's one: http://stackoverflow.com/questions/7681755/i-am-confused-about-php-post-redirect-get – Kai Qing Dec 05 '12 at 00:02
  • @DarylGill that's it: `header('HTTP/1.1 303 Optional Form');` and searches for anything like "optional form resubmit" don't get me anywhere – Raekye Dec 05 '12 at 00:02
  • @KaiQing I said I don't want to redirect. It's a small "contact me form" (leave me a message) that at most shows a "succesfully submitted" or "error: ..." on the same page. There's no page to go to afterwards, no result to show. – Raekye Dec 05 '12 at 00:04
  • the redirect reloads the page so you don't get the browser default message warning you about a re-post. If you think your form is so small then why not submit it via ajax and circumvent this problem entirely? – Kai Qing Dec 05 '12 at 00:10
  • Additionally, the reload in this case is no less transparent to the user than the initial form post anyhow, so why avoid it? – Kai Qing Dec 05 '12 at 00:13
  • I'm getting strange things with the `header('Location: ...')`... it doesn't seem to reload/submit the page at all (I'm redirecting to the same page). I hadn't thought of Ajax, but before I close this is there any way to submit a form to the same page, without getting the "this page requested data..." message? **Edit** I realized you probably meant create a new processing page, and redirect to the original one. Cool for me, I'll try it out. – Raekye Dec 05 '12 at 00:25
  • No, I meant to make the action blank or $_SERVER['PHP_SELF'] - as in
    - this assumes your form processing is at the top of the file before any output is written to the browser
    – Kai Qing Dec 05 '12 at 00:49
  • @KaiQing It's the same - no message when I refresh, but if I submit the form, then refresh, it keeps prompting me. Plus, In one of the threads, it said you shouldn't leave out the `action` in HTML5 – Raekye Dec 05 '12 at 03:55
  • Ah fixed, it was just URL problems because I was using my local host. – Raekye Dec 05 '12 at 04:24

3 Answers3

1

Your best option is to use Ajax. That way you don't need to reload. I usually use it with JQuery.
So you end up with something like:

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
        }
    });
}
Manatax
  • 4,083
  • 5
  • 30
  • 40
  • Accepted as an answer, but in case anyone wants the intended solution, (prevent resubmission, but without other scripts), see my final solution below – Raekye Dec 05 '12 at 04:24
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

Raekye
  • 5,081
  • 8
  • 49
  • 74
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.

Savageman
  • 9,257
  • 6
  • 40
  • 50
  • It's a 'leave me a message' form. Passing the entire message through get doesn't seem elegant.. – Raekye Dec 05 '12 at 00:04
  • 1
    Yeah I agree. The answer is not to use get. But rather to either capture post, then reload the referring page or submit it via ajax to avoid a page load – Kai Qing Dec 05 '12 at 00:12
  • What I meant is doing a redirect to issue a GET after the POST. – Savageman Dec 05 '12 at 21:53