How can PHP be used to redirect users to a different page based on form input?

To redirect users to a different page based on form input in PHP, you can use the header() function to send a raw HTTP header to the browser. You can check the form input using conditional statements and then use the header() function to redirect the user to the desired page.

<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $input = $_POST['input']; // Assuming 'input' is the name attribute of the form input field

    if($input == 'page1'){
        header("Location: page1.php");
        exit();
    } elseif($input == 'page2'){
        header("Location: page2.php");
        exit();
    } else {
        header("Location: error.php");
        exit();
    }
}
?>