What is the Post/Redirect/Get pattern and how can it be implemented in PHP forms?
The Post/Redirect/Get (PRG) pattern is a web development design pattern that helps prevent duplicate form submissions and improve user experience. It involves redirecting the user to a different URL after processing a form submission using the POST method. This prevents users from accidentally resubmitting the form data by refreshing the page.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Process form data
// Redirect to a different URL to prevent form resubmission
header("Location: success.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Form Example</title>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<!-- Form fields go here -->
<input type="submit" value="Submit">
</form>
</body>
</html>