Best way to avoid the submit due a refresh of the page
When a form submission triggers a database insert and returns a success message, refreshing the browser often leads to duplicate data appearing in your table. This happens because the server-side logic checks for the presence of the $_POST variable rather than verifying whether the action has already been completed. If your code simply looks like this:
if (isset($_POST['name'])) {
// Insert into database
echo "Operation Done";
die();
}
And the user hits F5 or clicks the refresh button, the browser reloads the same URL. Since $_POST data is preserved in the request on a page reload (depending on server configuration and how the form was originally submitted), the condition isset($_POST['name']) evaluates to true again, causing the script to re-execute the insertion logic.
To prevent this, you need to distinguish between an initial form submission and a subsequent refresh of the success page. The most robust solution is to check for a hidden field or a specific URL parameter that indicates the action was already performed.
One common pattern involves adding a hidden input field within your form:
<form action="page.php" method="post">
<input type="text" name="name" class="input400" />
<input type="hidden" name="action" value="submit" />
<input type="submit" name="Submit" value="Submit" />
</form>
On the processing side, you modify the logic to only act when that specific flag is present:
if (isset($_POST['action']) && $_POST['action'] == 'submit') {
// Perform database operation
echo "Operation Done";
}
When the page refreshes after the initial success, $_POST will still contain the hidden field value. However, if you redirect the user immediately after processingâusing a header redirect or return redirect() in frameworks like Laravelâthe browser loads a new URL without any POST data. In that scenario, refreshing simply reloads the result page, and since there is no POST data attached to the GET request, the condition fails safely.
Another approach relies on session state. You can set a flag in the user's session upon successful completion of the operation:
if (isset($_POST['name'])) {
// Insert into database
$_SESSION['operation_completed'] = true;
echo "Operation Done";
}
Then, at the very start of your script, check this flag before attempting any writes:
if (!isset($_SESSION['operation_completed']) && isset($_POST['name'])) {
// Insert into database
$_SESSION['operation_completed'] = true;
echo "Operation Done";
} else if (isset($_POST['name'])) {
echo "Action already completed. Refreshing is not allowed.";
}
This method works well even without a redirect, though it adds a slight overhead of managing session state for every request. It also requires ensuring that sessions are properly initialized and available on the page.
A third option is using the HTTP_X_REQUESTED_WITH header or checking the Accept header to differentiate between form submissions and direct refreshes, but this is less reliable across different browsers and server environments.
In modern frameworks like Laravel, the best practice is often handled by controllers and redirects. After performing a write operation, always redirect away from the current page using redirect()->back() or similar methods. This prevents the form data from being resubmitted on refresh because the redirected page is typically a simple view or a confirmation screen that does not accept POST data.
If you must keep the user on the same page after submissionâfor example, to display an error message alongside the success messageâyou must ensure your conditional logic explicitly guards against re-execution using either hidden fields, session flags, or database constraints like unique indexes combined with upsert logic.
Ultimately, relying solely on isset($_POST['field']) is insufficient for preventing duplicate submissions caused by accidental refreshes. Implementing a clear state transitionâeither via URL redirection or persistent session trackingâis essential to maintain data integrity and avoid polluting your database tables with unintended duplicates.