[英]Dropdown created via jquery ajax:how to get related items in second dropdown of selected value in first dropdown
I m trying to fill 2nd dropdown but its not filling with filter data but it fetches all states rather only related states according to country selected in first dropdown Please tell me whats wrong in code.Value of id cannot be found so all states name are filled in dropdown . 我正在尝试填充第二个下拉列表,但未填充过滤器数据,但它会提取所有州,而仅根据第一个下拉列表中选择的国家/地区获取相关州。请告诉我代码中出了什么问题。找不到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>
$.ajax({
type: "POST",
url: "getstate.php",
data: {country_id:id},
...
Edit 1: also 编辑1:也
var id=$('#country').val();
Edit 2: getstate.php 编辑2:getstate.php
include("connection.php");
$country_Id = intval($_POST['Country_Id']);//if country_Id is integer...if not:$country_Id = $_POST['Country_Id'];
$cntry = mysql_query("SELECT * FROM state where country_id=".$country_Id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.