What is the best way to handle form redirection in PHP based on user input?
When handling form redirection in PHP based on user input, you can use conditional statements to check the input and redirect the user accordingly. You can use header() function in PHP to redirect the user to a different page based on the input provided by the user.
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Process the form data
$userInput = $_POST['input'];
// Check user input and redirect accordingly
if($userInput == 'option1'){
header("Location: page1.php");
exit();
} elseif($userInput == 'option2'){
header("Location: page2.php");
exit();
} else {
header("Location: error.php");
exit();
}
}
?>
Related Questions
- What are the best practices for storing dates in a MySQL database to ensure accurate retrieval?
- What are the potential pitfalls when trying to establish a connection between IIS and MS SQL Server using PHP?
- What are some different approaches to outputting the next 7 days of the week starting from the current date in PHP?