问题
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
}
回答1:
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!
来源:https://stackoverflow.com/questions/22888777/i-need-help-finishing-this-3-level-dynamic-dropdown