我正在尝试使动态下拉框成为一种搜索工具,以帮助缩小来自 mysql 服务器的显示数据的范围。我是一个体面的 php 程序员,但需要有关 javascript 和 ajax 的帮助。
我将下面的代码用于带有 ajax 的下拉列表,但是当从第一个列表框中选择一种药水时,在下一个列表框中没有视图
请帮我?
ostan.php:
<head>
<script>
$(function (){ //document is loaded so we can bind events
$("#ali").change(function (){ //change event for select
$.ajax({ //ajax call
type: "POST", //method == POST
url: "getshahr.php", //url to be called
data: { ali: $("#ali option:selected").val()} //data to be send
}).done(function( msg ) { //called when request is successful msg
//do something with msg which is response
$("#txtHint").html(msg);
});
});
});
</script>
</head>
<?php
//db connection info.
$dbuser = 'root';
$dbhost = 'localhost';
$dbpass = '';
$dbname = 'tutorial';
$mysql_link = mysql_connect($dbhost,$dbuser,$dbpass) or die ("Could not connect");
mysql_select_db ($dbname) or die ("DB select failed"); //select db
$query = "select DISTINCT ostan from com"; //get product count by groups of products
$result = mysql_query($query); //queryresult1
echo '<select id="ali">';
while($row = mysql_fetch_array($result))
{
echo '<option id="'.$row['ostan'].'">'.$row['ostan'].'</option>';
}echo "</select><br /><br ><div id=\"txtHint\"><select name='select'>
<option>Select City</option>
</select></div>";
?>
getshahr.php:
<?php
//db connection info.
$dbuser = 'root';
$dbhost = 'localhost';
$dbpass = '';
$dbname = 'tutorial';
$mysql_link = mysql_connect($dbhost,$dbuser,$dbpass) or die ("Could not connect");
mysql_select_db ($dbname) or die ("DB select failed"); //select db
$query = "select shahr from com where ostan='{$_POST["ali"]}'";
$result = mysql_query($query); //queryresult1
echo '<select id="ali">';
while($row = mysql_fetch_array($result))
{
echo '<option id="'.$row['shahr'].'">'.$row['shahr'].'</option>';
}
echo "</select><br /><br />";
?>