What is the PRG (Post/Redirect/Get) pattern and how can it prevent duplicate form submissions in PHP?
The PRG (Post/Redirect/Get) pattern is a design pattern used to prevent duplicate form submissions in web applications. It works by redirecting the user to a different URL after a form submission, which prevents the user from refreshing the page and resubmitting the form data. In PHP, you can implement the PRG pattern by processing the form submission, redirecting the user to a new page using the header() function, and then displaying the success message or updated content on the redirected page.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Process form submission
// Redirect to a new page to prevent form resubmission
header("Location: success.php");
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Form Example</title>
</head>
<body>
<form method="post" action="">
<!-- Form fields go here -->
<input type="submit" value="Submit">
</form>
</body>
</html>
Related questions
-
array_rand PHP array
-
PHP file handling PDF
-
PHP HTML select database