How can PHP be used to dynamically display data based on user input, such as selecting an author from a dropdown menu?
To dynamically display data based on user input, such as selecting an author from a dropdown menu, you can use PHP in combination with HTML and JavaScript. You can create a form with a dropdown menu that lists all authors, and then use PHP to retrieve the selected author and fetch the corresponding data from a database. Finally, you can display the data on the page based on the user's selection.
<?php
// Assuming you have an array of authors and their corresponding data
$authors = [
'Author 1' => 'Data for Author 1',
'Author 2' => 'Data for Author 2',
'Author 3' => 'Data for Author 3'
];
// Check if a specific author is selected
if(isset($_POST['author'])){
$selected_author = $_POST['author'];
$data = $authors[$selected_author];
echo $data;
}
?>
<form method="post">
<select name="author">
<option value="Author 1">Author 1</option>
<option value="Author 2">Author 2</option>
<option value="Author 3">Author 3</option>
</select>
<input type="submit" value="Submit">
</form>