What is the best practice for opening a form in the same window after submission in PHP?
When submitting a form in PHP, it is a best practice to redirect back to the same page after processing the form data to prevent resubmission on page refresh. This can be achieved by using the header() function to redirect back to the same page.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Process form data here
// Redirect back to the same page to prevent form resubmission
header("Location: ".$_SERVER['PHP_SELF']);
exit();
}
?>
<!-- Your HTML form goes here -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<!-- Form fields go here -->
<input type="submit" value="Submit">
</form>
Related Questions
- How can data be efficiently displayed in a table format in PHP without the need for multiple files?
- What are the differences in enabling OCI for PHP between CLI and httpd on a CentOS server?
- What steps can be taken to ensure compatibility with different PHP versions when using array functions like array_push?