I have been stuck up at the point of form data clearing. After the user submits a form the data is added to database. Its fine till here. But if the user refreshes the page previously entered data is re entered into the database. I am using post method to send the data. I am not using AJAX. The whole page reloads after the for data is submitted. Please help me with this. If there is any way to clear form data after submiting it, so that after refreshing the page no data is added to database do reply to this question.
-
Maybe use a boolean to check before submitting, then when submitted set it to true? – Emerion Sep 29 '10 at 13:19
8 Answers
After inserting the data, do a redirect:
header('Location:'.$_SERVER['PHP_SELF']);
After redirecting, refreshing won't cause data to be inserted again.
To show the message to the user I would simply set a flag via $_GET
. For example:
// Redirect and set $_GET variable
header('Location:'.$_SERVER['PHP_SELF'].'?showmsg=true');
To show the hidden div you could make a function:
function show_confirmation()
{
return isset($_GET['showmsg']) && $_GET['showmsg'] == 'true';
}
And use the function like this:
<div style="display:<?php echo show_confirmation() ? 'block' : 'none'; ?>">
<!-- Message for user -->
</div>

- 42,876
- 8
- 99
- 111
-
Thanks dude it works. but there's another problem. I have got a message to display after the submission of the form. The message is displayed inside a div element which is hidden before the user submits the form. Can you help regarding this? – Sanket Raut Sep 30 '10 at 04:16
-
Sanket, I updated my answer with a suggestion for showing the hidden div to your users. – Mischa Sep 30 '10 at 04:43
It seems to me that the post action of your form is the form page itself. You have to add a check whether the form has already been submitted, so that you the data is not again saved in the database. Or you could change the post action to a different page that does the insertion in the database and redirects the user to the initial page form.

- 1,179
- 1
- 16
- 35
If you use POST
instead of GET
as the action property on your form, then refreshing the result page won't resubmit the form (some browsers may offer the option to do so, but they certainly shouldn't do so by default).
However, you can't rule out the possibility of a form being posted more than once -- there's several common reasons why it happens; eg if the page is slow to load, the user may click more than once on the button.
The best thing to do is to have a hidden unique ID field on the form, so you can tell when you're receiving a form that has been submitted more than once, and your program can ignore it.

- 166,037
- 39
- 233
- 307
After inserting it to database, call unset() method to clear the data.
unset($_POST);
check my answer here: https://stackoverflow.com/a/37573913/1828764
Most people use a url to POST the form to, and then redirect back to the original form. This can be same url.

- 93,428
- 18
- 118
- 189
Try using sessions. Set a session variable to record whether the user has already submitted the form.
Put this at the beginning of your code (this needs to be on every page):
session_start();
And then when they submit the form, set a session variable:
$_SESSION['form_submitted'] = true;
Here's a link to a tutorial on sessions: Tutorial

- 2,545
- 1
- 24
- 38
Generate a unique key when generating the form html and set a hidden field in the form to this key. Save the key along with the data and don't save the data if the key is already present in the database.

- 6,523
- 2
- 34
- 49
If you choose the redirect method don't forget to add a die after redirect to instruct webserver to stop script execution.
if(saveFormInformation())
{
header('Location: http://www.example.com/'); //this line instructs browser
//to go to example. com
//but the webserver will keep executing the rest of the script
//unless you instruct it to stop
die; //this line instructs web server to stop execution of the script
}

- 89
- 1