【发布时间】:2014-04-06 01:01:17
【问题描述】:
我知道还有很多基于这个主题的其他主题。我的代码基于其中之一。它工作得很好。唯一的问题是它是一个 2 级下拉菜单。我需要添加另一个下拉列表,以便完成我的国家、地区、城市列表。
如果您可以查看我的代码并建议我如何添加第三个下拉列表(城市),那就太好了。
index.php
<html>
<head>
<script>
function showHint(str) {
var xmlhttp;
if (str.length==0) {
document.getElementById("regiondiv").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("regiondiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax.php?country="+str,true);
xmlhttp.send(null);
}
</script>
</head>
<body>
<div class="field">
<label for="cat">Country</label>
<select id="country" name="country" onChange="showHint(this.value);" required >
<option value="0">--Select Country--</option>
<?php
$getCountry = DB::getInstance()->query("SELECT * FROM countries");
if(!$getCountry->count()) {
echo 'No Country found!';
} else {
foreach($getCountry->results() as $row) {
$country_id = escape($row->countryId);
$country_name = escape($row->countryName);
?><option value="<?php echo $country_id; ?>" ><?php echo $country_name; ?></option><?php
}
}
?>
</select>
</div>
<label for="cat">Region</label>
<div id="regiondiv">
<select name="region" id="region">
<option>--Select State--</option>
<option></option>
</div>
</body>
</html>
ajax.php
<?php require_once 'core/init.php';
$country_id = escape(Input::get('country'));
$select_region = DB::getInstance()->get('regions', array('countryId', '=', $country_id));
if(!$select_region->count()) {
echo 'No Country found!';
} else {
?><select name="region" id="region"><?php
foreach($select_region->results() as $row) {
$region_id = escape($row->regionId);
$region_name = escape($row->regionName);
?><option value="<?php echo $region_id; ?>" ><?php echo $region_name; ?></option><?php
}
?></select><?php
}
【问题讨论】:
-
您将从哪里获取城市数据?
-
来自mysql表,同国家和地区。
-
您能否更具体地说明一下这张城市表的外观?有与之关联的区域 ID 吗? ^^
标签: php mysql ajax dynamic location