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();
}
}
?>
Keywords
Related Questions
- How does PHP script know where to look for the database on localhost when connecting?
- What considerations should be taken into account when allowing custom file extensions for uploads in PHP to ensure proper validation?
- What is the difference between printf and sprintf when truncating floating point numbers in PHP?