Skip to content
PHP Logo

How can PHP scripts handle form submissions and redirect users to a different page after submission?

To handle form submissions in PHP and redirect users to a different page after submission, you can use the header() function to set the Location header to the URL of the page you want to redirect to. This should be done before any output is sent to the browser to avoid any errors.

php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data here

    // Redirect user to a different page
    header("Location: newpage.php");
    exit();
}
?>