"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to prevent duplicate submissions after form refresh?

How to prevent duplicate submissions after form refresh?

Posted on 2025-05-04
Browse:647

How to Prevent Duplicate Submissions After Form Refresh?

Preventing Duplicate Submissions with Refresh Handling

In web development, it's common to encounter the issue of duplicate submissions when a page is refreshed after a form submission. To address this, consider the following approach:

Imagine having a code snippet that looks something like this:



When you submit this form, the provided data is inserted into the database, and a success message is displayed. However, if you were to refresh the page, the data would be re-submitted to the database.

Solution: Redirect After Submission

To avoid this duplication, it's crucial that after the operation is complete, you do not display the response on the same page. Instead, redirect to a new page.

// Database operations...
// Set success flash message. (Assuming you're using a framework)
header('Location: /path/to/record');
exit;

By redirecting after the submission, you ensure that refreshing the page will only trigger the GET request for the new page that you redirected to, preventing duplicate submissions.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3