我已附加代码以从DB获取动态下拉列表。当用户使用onChange()从第一个下拉列表中选择时,将根据第一个下拉列表的选择检索第二个下拉列表的数据。但是,我可以获得第一个下拉列表但没有第二个下拉列表的结果。似乎ajax没有调用。
另一个问题:
我可以为第二个下拉列表制作单选按钮
谢谢
<?php
echo"1"
if(isset($_POST['get_option']))
{
$host = 'localhost';
$user = 'root';
$pass = 'root';
mysql_connect($host, $user, $pass);
mysql_select_db('db_photography');
$state = $_POST['get_option'];
$find=mysql_query("select city from places where state='$state'");
while($row=mysql_fetch_array($find))
{
echo"1";
echo "<option>".$row['city']."</option>";
}
exit;
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="select_style.css">
<script type="text/javascript" src="jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
function fetch_select(val)
{
$.ajax({
type: 'post',
url: 'fetch_data.php',
data: {
get_option:val
},
success: function (response) {
document.getElementById("new_select").innerHTML=response;
}
});
}
</script>
</head>
<body>
<p id="heading">Dynamic Select Option Menu Using Ajax and PHP</p>
<center>
<div id="select_box">
<select onchange="fetch_select(this.value);">
<option>Select state</option>
<?php
$host = 'localhost';
$user = 'root';
$pass = 'root';
mysql_connect($host, $user, $pass);
mysql_select_db('db_photography');
$select=mysql_query("select state from places group by state");
while($row=mysql_fetch_array($select))
{
echo "<option>".$row['state']."</option>";
}
?>
</select>
<select id="new_select">
</select>
</div>
</center>
</body>
</html>