In the context of PHP development, how can the concept of conditional page navigation be implemented to guide users through a multi-page questionnaire based on their input validation status?
To implement conditional page navigation in a multi-page questionnaire based on input validation status, you can use PHP to check the validation status of the user input on each page. Depending on the validation status, you can redirect the user to the next appropriate page or display an error message.
<?php
session_start();
// Check if form is submitted
if(isset($_POST['submit'])) {
// Perform input validation
$input = $_POST['input'];
if($input == 'valid') {
// Redirect to next page
header('Location: page2.php');
exit();
} else {
// Display error message
$_SESSION['error'] = 'Invalid input. Please try again.';
}
}
?>
<html>
<body>
<form method="post" action="page1.php">
<input type="text" name="input" required>
<button type="submit" name="submit">Submit</button>
</form>
<?php
// Display error message if exists
if(isset($_SESSION['error'])) {
echo $_SESSION['error'];
unset($_SESSION['error']);
}
?>
</body>
</html>
Related Questions
- How can using backticks instead of single quotes in SQL queries impact the execution of the query in PHP?
- In what scenarios should the accept-charset attribute be used in HTML forms to ensure correct handling of special characters in PHP applications?
- What is the correct way to display the content of a text file in PHP?