使用PHP MySql JQuery将数据检索到Ajax下拉菜单中 [英] Retrieve data into ajax drop-down menu using PHP MySql JQuery
问题描述
我使用Jquery/PHP/MySql
创建了动态的多个国家/地区下拉列表.这样工作正常,对于MySql数据库中的每个用户,我的store/put
国家/州/镇都像这样(usertable)
:
I created dynamic multiple country dropdown using Jquery/PHP/MySql
. this worked fine and i store/put
country/state/town for each user in MySql Database like this (usertable)
:
id | country | state | town |
1 | 1 | 1 | 1 |
2 | 1 | 1 | 3 |
3 | 1 | 2 | 8 |
现在在edituser.php
页面中,我需要fetch/retrieve
MySql数据(usertable)
到我的Jquery/Ajax下拉列表中.我打电话给edituser.php?id=1
,现在我需要将用户数据打印/显示到dropdown Ajax
以便编辑国家/州/镇用户.
now in edituser.php
page I need to fetch/retrieve
MySql data (usertable)
to my Jquery/Ajax dropdown. i call edituser.php?id=1
Now I need to print/show user data to dropdown Ajax
for edit country/state/town user.
如何检索/打印/显示此内容?
JS:
<script type="text/javascript" src="js/jquery-1.4.1.min.js"></script>
// jquery library file
<script type="text/javascript">
/*This function is called when state dropdown value change*/
function selectState(state_id){
if(state_id!="-1"){
loadData('city',state_id);
}else{
$("#city_dropdown").html("<option value='-1'>Select city</option>");
}
}
/*This function is called when city dropdown value change*/
function selectCity(country_id){
if(country_id!="-1"){
loadData('state',country_id);
$("#city_dropdown").html("<option value='-1'>Select city</option>");
}else{
$("#state_dropdown").html("<option value='-1'>Select state</option>");
$("#city_dropdown").html("<option value='-1'>Select city</option>");
}
}
/*This is the main content load function, and it will
called whenever any valid dropdown value changed.*/
function loadData(loadType,loadId){
var dataString = 'loadType='+ loadType +'&loadId='+ loadId;
$("#"+loadType+"_loader").show();
$("#"+loadType+"_loader").fadeIn(400).
html('Please wait... <img src="image/loading.gif" />');
$.ajax({
type: "POST",
url: "loadData.php",
data: dataString,
cache: false,
success: function(result){
$("#"+loadType+"_loader").hide();
$("#"+loadType+"_dropdown").
html("<option value='-1'>Select "+loadType+"</option>");
$("#"+loadType+"_dropdown").append(result);
}
});
}
</script>
HTML:
/*This code will show country dropdown list*/
<select onchange="selectCity(this.options[this.selectedIndex].value)">
<option value="-1">Select country</option>
<?php
while($rowCountry=mysql_fetch_array($resCountry)){
?>
<option value="<?php echo $rowCountry['id']?>">
<?php echo $rowCountry['country_name']?>
</option>
<?php
}
?>
</select>
/*State dropdown list*/
<select id="state_dropdown"
onchange="selectState(this.options[this.selectedIndex].value)">
<option value="-1">Select state</option>
</select>
<span id="state_loader"></span>
/*City dropdown list*/
<select id="city_dropdown">
<option value="-1">Select city</option>
</select>
<span id="city_loader"></span>
Loaddata.php
Loaddata.php
include('dbConnect.inc.php');
$loadType=$_POST['loadType'];
$loadId=$_POST['loadId'];
if($loadType=="state"){
$sql="select id,state_name from state_test where
country_id='".$loadId."' order by state_name asc";
}else{
$sql="select id,city_name from city_test where
state_id='".$loadId."' order by city_name asc";
}
$res=mysql_query($sql);
$check=mysql_num_rows($res);
if($check > 0){
$HTML="";
while($row=mysql_fetch_array($res)){
$HTML.="<option value='".$row['id']."'>".$row['1']."</option>";
}
echo $HTML;
}
推荐答案
我认为Kendo UI下拉小部件将解决此问题.上面有一个层叠功能.
I think the Kendo UI dropdown widget will solve this problem. There is a cascade feature on it.
这篇关于使用PHP MySql JQuery将数据检索到Ajax下拉菜单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!