How can the PHP code be modified to ensure that after form submission, the form is completely cleared of all input data?
To ensure that the form is completely cleared of all input data after submission, you can use the header() function to redirect the user back to the same page after processing the form data. This will reload the page and clear any input data that was previously submitted.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Process form data here
// Redirect to the same page to clear input data
header("Location: ".$_SERVER['PHP_SELF']);
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Clear Form After Submission</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<!-- Form fields go here -->
<input type="text" name="name" placeholder="Name">
<input type="email" name="email" placeholder="Email">
<button type="submit">Submit</button>
</form>
</body>
</html>
Keywords
Related Questions
- What could be causing the issue of displaying "Index of..." instead of the PHP message after installing Apache server?
- Are there any specific considerations to keep in mind when working with time zones in PHP date conversions?
- What are the best practices for handling user authentication in a PHP application when interacting with external APIs like Facebook's login system?