How can PHP be used to dynamically display images and text based on user selections from dropdown menus on a form?
To dynamically display images and text based on user selections from dropdown menus on a form, you can use PHP in conjunction with HTML and JavaScript. You can create an array mapping the dropdown menu options to corresponding images and text, then use JavaScript to update the displayed content based on the user's selection. PHP can be used to generate the initial HTML form and handle any backend processing needed.
<?php
// Define an array mapping dropdown menu options to corresponding images and text
$dropdown_options = array(
"Option 1" => array("image" => "image1.jpg", "text" => "Text for Option 1"),
"Option 2" => array("image" => "image2.jpg", "text" => "Text for Option 2"),
"Option 3" => array("image" => "image3.jpg", "text" => "Text for Option 3")
);
// Check if form has been submitted
if(isset($_POST['dropdown'])){
$selected_option = $_POST['dropdown'];
$selected_image = $dropdown_options[$selected_option]['image'];
$selected_text = $dropdown_options[$selected_option]['text'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Dynamic Image and Text Display</title>
</head>
<body>
<form method="post">
<select name="dropdown">
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
</select>
<input type="submit" value="Submit">
</form>
<?php
// Display selected image and text
if(isset($selected_option)){
echo "<img src='$selected_image' alt='Selected Image'>";
echo "<p>$selected_text</p>";
}
?>
</body>
</html>
Keywords
Related Questions
- How can the ACL system provided in the forum thread be integrated into the existing PHP code to control access to specific pages based on user roles and permissions?
- How can PHP be used to prevent duplicate entries in a registration form?
- What are common issues with MIME type detection during file uploads in PHP?