In PHP, what are the recommended methods for clearing form fields after successful submission to prevent resubmission via the back button?
When a form is submitted successfully, it is common practice to redirect the user to a different page to prevent resubmission if the back button is pressed. To clear form fields after successful submission, you can use JavaScript to reset the form fields or PHP to unset the form data variables.
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Process the form data
// Redirect to a different page to prevent resubmission
header("Location: success.php");
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Clear Form Fields After Submission</title>
</head>
<body>
<form method="post" action="">
<input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>" placeholder="Name">
<input type="email" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>" placeholder="Email">
<button type="submit">Submit</button>
</form>
</body>
</html>
Keywords
Related Questions
- When should mysql_real_escape_string be used in PHP database queries?
- What are some best practices for optimizing the display of multiple pages of content on a PHP website, such as a guestbook or forum?
- What are the potential pitfalls of using file_exists with remote files and what alternative approach can be used?