0

I have a form in a page .Now I am submitting the form to the same page(using post).This part works well. But when I refresh the page, the form is submitted again.Any clues on how to fix this.

Aditya Shukla
  • 13,595
  • 12
  • 38
  • 36
  • You can use redirection or you can use form nonce to avoid duplication – shankhan Jan 06 '11 at 16:28
  • Possible duplicates: http://stackoverflow.com/questions/3923904/preventing-form-resubmission , http://stackoverflow.com/questions/880437/preventing-double-form-submissions , http://stackoverflow.com/questions/218907/how-to-handle-multiple-submissions-server-side ...etc... – Yahel Jan 06 '11 at 16:35

4 Answers4

3

Do a header redirect to either the same page or another page.

simshaun
  • 21,263
  • 1
  • 57
  • 73
  • I am trying to do a redirect , but the issue is the page is separated in a header - body-footer.So when i try to redirect it gives me a warning :header already sent at (header.php) pointing to the header I have in the page.How to handle this? – Aditya Shukla Jan 06 '11 at 16:48
  • Headers must come before any output (even a single space will cause that warning). I highly recommend putting the header at the top of the page (in a condition), but you could also turn on output buffering beforehand. Be aware that it causes higher CPU load though. – simshaun Jan 06 '11 at 16:55
2

Redirect back to the same page after processing the POST request.

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    # process the request ...

    # ... and redirect
    header('Location: page.php');
}
Ree
  • 6,061
  • 11
  • 48
  • 50
0

You can work with sessions. If the form name exists in the session, you don't handle the form again.

$_SESSION['form']['name_of_the_form'] = time();
Stijn Leenknegt
  • 1,317
  • 4
  • 12
  • 22
0

As the others have noted, you can use a HTTP redirect by modifying the headers sent back to the user's browser. The header() function in PHP allows you to do this. Be sure that you execute this function call prior to outputting anything, otherwise it will fail. It could look something like the following.

<?php
// some code to process the form data here
header('Location: page.php'); // redirect user to page.php
Kyle
  • 2,822
  • 2
  • 19
  • 24
  • I am trying to do a redirect , but the issue is the page is separated in a header - body-footer.So when i try to redirect it gives me a warning :header already sent at (header.php) pointing to the header I have in the page.How to handle this? – Aditya Shukla Jan 06 '11 at 16:53