问题描述
我知道有基于此课题的其他主题吨。我的code基于其中之一。它的伟大工程。它唯一的问题是,它是一个2级下拉。我需要添加另一个下拉这样我就可以完成我的国家,地区,城市列表。
如果你可以看看我的code和建议我如何添加第三个下拉列表(市),这将是美好的。
的index.php
< HTML>
< HEAD>
<脚本>
功能showHint(STR){
VAR XMLHTTP;
如果(str.length == 0){
。的document.getElementById(regiondiv)的innerHTML =;
返回;
}
如果(window.XMLHtt prequest){
XMLHTTP =新XMLHtt prequest();
}
其他 {
XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange =功能(){
如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){
的document.getElementById(regiondiv)的innerHTML = xmlhttp.responseText。
}
}
xmlhttp.open(GET,?ajax.php国家=+ STR,真正的);
xmlhttp.send(空);
}
< / SCRIPT>
< /头>
<身体GT;
< DIV CLASS =字段>
<标签=猫>国家< /标签>
<选择一个id =国家名称=国家的onChange =showHint(THIS.VALUE);需要>
<期权价值=0> - 选择国家 - < /选项>
< PHP
$ getCountry = DB ::的getInstance() - >查询(SELECT * FROM国家);
如果($ getCountry->!计数()){
回声没有国家找到了!;
} 其他 {
的foreach($ getCountry->结果()为$行){
$ COUNTRY_ID =逃生($行向> countryId);
$ COUNTRY_NAME =逃生($行向>国家名称);
?><期权价值=< PHP的echo $ COUNTRY_ID;>中>< PHP的echo $ COUNTRY_NAME;其中&;?>< /选项&GT?PHP
}
}
?>
< /选择>
< / DIV>
<标签=猫>地区< /标签>
< DIV ID =regiondiv>
<选择名称=区域ID =区域>
<选项> - 选择国家 - < /选项>
<选项>< /选项>
< / DIV>
< /身体GT;
< / HTML>
ajax.php
< PHP require_once'核心/的init.php;
$ COUNTRY_ID =逃生(输入::获得(国家));
$ select_region = DB ::的getInstance() - >获得('地区',阵列('countryId','=',$ COUNTRY_ID));
如果($ select_region->!计数()){
回声没有国家找到了!;
} 其他 {
其中&;?><选择名称=区域ID =区域&GT?PHP
的foreach($ select_region->结果()为$行){
$ REGION_ID =逃脱($行向> regionId);
$ =的region_name逃生($行向> regionName);
?><期权价值=< PHP的echo $ REGION_ID;>中>< PHP的echo $的region_name;其中&;?>< /选项&GT?PHP
}
?>< /选择>< PHP
}
我真的不明白你的问题呢?你基本上是问怎么办,现在你在做什么的地区再次该市?
像创建一个cityAjax.php:
< PHP require_once'核心/的init.php;
$ REGION_ID =逃脱(输入::得到('区域'));
$ select_city = DB ::的getInstance() - >获得(全市',阵列('regionId','=',$ REGION_ID));
如果($ select_city->!计数()){
回声没有城市找到了!;
} 其他 {
其中&;?><选择名称=城市ID =城与GT?PHP
的foreach($ select_city->结果()为$行){
$ city_id =逃生($行向> cityId);
$ CITY_NAME =逃生($行向>的cityName);
?><期权价值=< PHP的echo $ city_id;>中>< PHP的echo $ CITY_NAME;其中&;?>< /选项&GT?PHP
}
?>< /选择>< PHP
}
我很抱歉,但你的问题并没有真正意义......
更新:
嗯,我想我明白你的问题。你不明白如何让生成的区域选择加载一个城市的部分。
好了,你可以做这样的:
-
就像提到的进一步上涨,你首先需要创建一个新的ajax.php检索城市的逻辑。这将是更好的从一个文件句柄,而是让事情变得简单,我们将只去了第二个PHP文件。
-
您还需要扩展基本HTML文件,并添加一个占位符城市:
... 地区 - 选择State--
<标签=猫>城市< /标签> < DIV ID =citydiv> <选择名称=城市ID =城与GT; <选项> - 选择国家 - < /选项> <选项>< /选项> < / DIV>
...
-
您还需要添加一个JavaScript函数来填充这个新的占位符:
函数showHint(STR){
...
xmlhttp.open(GET,?ajax.php国家=+ STR,真正的);
xmlhttp.send(空);
...
}
功能showCity(STR){
VAR XMLHTTP;
如果(str.length == 0){
。的document.getElementById(citydiv)的innerHTML =;
返回;
}
如果(window.XMLHtt prequest){
XMLHTTP =新XMLHtt prequest();
} 其他 {
XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange =功能(){
如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){
的document.getElementById(citydiv)的innerHTML = xmlhttp.responseText。
}
}
xmlhttp.open(GET,?cityAjax.php区=+ STR,真正的);
xmlhttp.send(空);
}
: - 最后但并非最不重要的,你需要扩展现有ajax.php code,调用新的JavaScript功能,如果选择的区域:
...
?><选择名称=区域ID =区域的onChange =showCity(THIS.VALUE);要求>< PHP
的foreach($ select_region->结果()为$行){
$ REGION_ID =逃脱($行向> regionId);
$ =的region_name逃生($行向> regionName);
?><期权价值=< PHP的echo $ REGION_ID;>中>< PHP的echo $的region_name;其中&;?>< /选项&GT?PHP
}
?>< /选择>< PHP
...
希望这有助于一点!
I know there are tons of other topics based on this subject. My code is based on one of them. It works great. The only issue with it is that it's a 2 level dropdown. I need to add another dropdown so I can complete my Country, Region, City list.
If you can look at my code and advice me how to add the 3rd dropdown(cities), that would be wonderful.
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
}
I don't really understand your problem then? You are basically asking how to do exactly what you are doing now for the Regions again for Citys?
Like creating a cityAjax.php:
<?php require_once 'core/init.php';
$region_id = escape(Input::get('region'));
$select_city = DB::getInstance()->get('citys', array('regionId', '=', $region_id));
if(!$select_city->count()) {
echo 'No City found!';
} else {
?><select name="city" id="city"><?php
foreach($select_city->results() as $row) {
$city_id = escape($row->cityId);
$city_name = escape($row->cityName);
?><option value="<?php echo $city_id; ?>" ><?php echo $city_name; ?></option><?php
}
?></select><?php
}
I'm sorry, but your question doesn't really make sense...
Update:
Ah, I think I understand your problem. You don't understand how to let the generated regions selection load a city section.
Ok, you could do it like this:
like mentioned further up, you would first need to create a new ajax.php for the logic of retrieving the cities. It would be nicer to handle all from one file, but to keep things simple we'll just go with a second php file.
You will also need to extend your base html file and add a placeholder for the cities:
... Region --Select State--
<label for="cat">Cities</label> <div id="citydiv"> <select name="city" id="city"> <option>--Select State--</option> <option></option> </div>
...
You will also need to add a javascript function to populate this new placeholder:
:
function showHint(str) {
...
xmlhttp.open("GET","ajax.php?country="+str,true);
xmlhttp.send(null);
...
}
function showCity(str) {
var xmlhttp;
if (str.length==0) {
document.getElementById("citydiv").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("citydiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","cityAjax.php?region="+str,true);
xmlhttp.send(null);
}
:- Last but not least, you will need to extend your existing ajax.php code, to call the new javascript function if a region is selected:
...
?><select name="region" id="region" onChange="showCity(this.value);" required><?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
...
Hope this helps a bit!
这篇关于我需要帮助整理这3级动态下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!