How can a PHP script prevent data from being resubmitted to the database when a page is reloaded?
When a page is reloaded, a PHP script can prevent data from being resubmitted to the database by using a technique called "post/redirect/get." This involves processing the form submission, storing the data in the database, and then redirecting the user to a different page using the header function. This way, if the user refreshes the page, the form data will not be resubmitted.
// Process form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Store data in the database
// Redirect to a different page to prevent form resubmission
header("Location: success.php");
exit;
}
Related Questions
- In what way does modifying the code irregularly, as seen in the provided example, impact the functionality of Zend Framework 2?
- What potential pitfalls can arise when executing SQL queries in PHP code compared to directly in a database?
- What is the purpose of using the PHP header function for page reloading?