How can a PHP function be used to populate a second dropdown menu based on the selection in the first dropdown menu?
To populate a second dropdown menu based on the selection in the first dropdown menu, you can use AJAX to dynamically fetch the options for the second dropdown menu based on the selected value from the first dropdown menu. When a user selects an option from the first dropdown menu, an AJAX request is sent to a PHP script that fetches the corresponding options for the second dropdown menu and returns them as a response. The response is then used to populate the second dropdown menu.
<?php
if(isset($_POST['selected_value'])) {
$selected_value = $_POST['selected_value'];
// Perform database query or any other logic to fetch options for the second dropdown based on the selected value
// Example response data
$options = array(
'Option 1',
'Option 2',
'Option 3'
);
// Return the options as JSON
echo json_encode($options);
exit;
}
?>