markzzz markzzz - 3 months ago 5
HTML Question

Best way to avoid the submit due to a refresh of the page

I think that this problem happens often on a web application. But I'll explain in details my troubles.

I'd like to know how to get rid of this behaviour, for example when I've a code like this :

<?
if (isset($_POST['name'])) {
... operation on database, like to insert $_POST['name'] in a table ...
echo "Operation Done";
die();
}

?>

<form action='page.php' method='post' name="myForm">
<input type="text" maxlength="50" name="name" class="input400" />
<input type="submit" name="Submit" />
</form>


When I submit the form, I insert the data on database, and I get the message Operation Done. Than, if I refresh the page, the data will insered again.

How can I avoid from this situation? Any suggestions will be good :)

Answer

Don't show the response after your create action; redirect to another page after the action completes instead. If someone refreshes, they're refreshing the GET requested page you redirected to.

// submit
// set success flash message (you are using a framework, right?)
header('Location: /path/to/record');
exit;
Comments