我正在尝试填写第二个下拉列表,但它没有填充过滤器数据,但它根据第一个下拉列表中选择的国家/地区获取所有州,而不是仅相关州请告诉我代码中有什么问题。找不到 id 的值,因此所有州名都已填写在下拉列表中。
<script type="text/javascript" src="jquery-ajax/jquery.js"></script>
<script>
$(document).ready(function(){
$("#country").change(function(){
//var optionValue = $("select[name='country_select']").val();
var id=$("#country").val();
//var dataString = 'id='+ id;
//alert("datastring"+dataString);
$.ajax({
type: "POST",
url: "getstate.php",
data: {Country_Id:id},
beforeSend: function(){ $("#ajaxLoader").show(); },
complete: function(){ $("#ajaxLoader").hide(); },
success: function(response){
$("#stateAjax").html(response);
$("#stateAjax").show();
}
});
});
});
</script>
</head>
<?php
include("connection.php");
?>
<body>
<form method="post">
Select Country:<select name="country_select" id="country">
<option value="">Select Country</option>
<?php
$cntry = mysql_query("SELECT Country_Id, name FROM country ORDER BY name ASC");
while($row = mysql_fetch_array($cntry))
{
$id=$row['Country_Id'];
$name=$row['name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
?>
</select>
<span id="errmsg" style="display:none">There is no matching option</span>
<div id="ajaxLoader" style="display:none"><img src="jquery-ajax/ajax-loader.gif" alt="loading..."></div>
<div id="stateAjax" style="display:none">
<select name="state_select" id="state" style="display:none">
<option value="">Please Select</option></select></select>
</div>
</form>
</body>
getstate.php
<?php
echo $_POST['id'];
include("connection.php");
$cntry = mysql_query("SELECT * FROM state where country_id=".$_POST['id']);
?>
State:
<select name="state_select" id="state">
<option value="">Please Select</option>
<?php
while($row = mysql_fetch_array($cntry))
{
$id=$row['State_id'];
$name=$row['name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
?>
</select>