I need to be able to stop a page from posting once the data in it has been submitted. For example I could have this form:
<form action='#' method='post'>
<input type='text' name='name' />
<input type='submit' name='submit' />
</form>
Then catch the post like this:
<?php
if (isset($_POST['submit'])) {
echo "Hello " . $_POST['name'];
}
?>
But when the page refreshes it will preform the same action time and again until either the user leaves the page or does a full refresh on the page. This is causing me a huge issue as it is imperative I am able to stop the post from preforming once it has been done.
I have tried using a meta tag (<meta http-equiv="refresh" content="5">
) to refresh the page, but this caused an issue as every few seconds it would do it and the page data was lost, I have tried to unset the post variable (unset($_POST['submit']);
and unset($_POST);
), but this did not work, I cannot use header("Location: ...") as I am using WordPress and thus headers are always sent by the system.
So I ask this of the community we have: How can I stop the post from re-submitting the data?
EDIT
What I am now doing:
<?php
if (!isset($_SESSION)) {
session_start();
}
$token = rand();
if ($_POST) {
if (isset($_SERVER['token'])) {
if($_POST['token'] == $_SESSION['token']){
$continue = FALSE;
}
else {
$continue = TRUE;
}
}
else {
$_SERVER['token'] = $_POST['token'];
$continue = TRUE;
}
if ($continue === TRUE) {
if (isset($_POST['update'])) {
//Do update code
}
if (isset($_POST['win'])) {
//Do win code
}
if (isset($_POST['credit'])) {
//do credit code
}
}
}
Form:
<form method="post" action="#">
<select name="select">
<option value="false" <? if ($results->data[$i]['marked'] == "false") { echo "selected"; } ?>>No Attention</option>
<option value="true" <? if ($results->data[$i]['marked'] == "true") { echo "selected"; } ?>>Needs attention</option>
</select>
<input type="hidden" name="token" value="<?php echo $token; ?>" />
<input type="hidden" name="betid" value="<? echo $results->data[$i]['bet_id']; ?>" />
<input name="update" id="" class="btn btn-default" value="Update" type="submit">
</form>
You can do it like this:
Then catching the post
It can properly be made smaller / more compact but you should get the idea behind the code, this way if a user who has already submitted data can submit new data if he access the form a new, but if the page refreshes the data won't be procesed again
Of course you have to be sure that SESSION is startet
This is a perfect question to link one of my favourite answers on Stackexchange: https://softwareengineering.stackexchange.com/questions/46716/what-technical-details-should-a-programmer-of-a-web-application-consider-before
This answer also says:
I do recommend you to click on the link.
So basically to redirect after a post has been submitted. Instead of posting to the page you go to now when you post information, you post to a redirect that redirects to the page you want to go. This will make sure when you refresh, it wont post the information again.