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
- Are there best practices for efficiently retrieving and storing location data in PHP for future use?
- What are the potential pitfalls of using the LIKE operator in MySQL queries?
- What are the best practices for integrating checkboxes and icons with PHP scripts to enable actions like update, insert, replace, or delete in a database?