- Menu scripts
- Window scripts
- Calendar scripts
- Gallery scripts
- Form widgets
- Tab view scripts
- Table widgets
- Drag and drop
- Folder trees
- Tooltips
- AJAX scripts
- Content Effects

- Misc scripts
- Javascript games
- Chess widgets
- DHTML Suite
- Resources
- cbolson.com
Download AJAX chained select
Licensing
This script is distributed under the LGPL open source license.
Commercial licenses are also available. Some of these licenses also includes personal e-mail support for up to 1 year.
PHP
This script requires PHP. You can also other server side languages if you rewrite the code for getCities.php(See below)
Put this into your <HEAD> section
Put this into your <BODY> section
Download Javascript file
I have used the Simple Ajax Code Kit library(SACK) for this script. I have made a copy available for download here.
Put ajax.js in a subfolder called "js" or change the path to the file in the code above(i.e. <script type="text/javascript" src="js/ajax.js"></script>)
Configuration
This script works like this:
- Someone select a country from the select box. This triggers the function getCityList()
- Ajax executes the file getCities.php to get a list of cities from the selected country
- The script executes the response from Ajax, i.e. creates a list of cities in the second select box
getCities.php
Ajax sends a request to the file getCities.php which returns a list of cities. A server side script like this will usually return data from a database. However, I have in this demo used static data to make things simple.
Here's the content of getCities.php:
<?php
if(isset($_GET['countryCode'])){
switch($_GET['countryCode']){
case "no":
echo "obj.options[obj.options.length] = new Option('Bergen','1');\n";
echo "obj.options[obj.options.length] = new Option('Haugesund','2');\n";
echo "obj.options[obj.options.length] = new Option('Oslo','3');\n";
echo "obj.options[obj.options.length] = new Option('Stavanger','4');\n";
break;
case "dk":
echo "obj.options[obj.options.length] = new Option('Aalborg','11');\n";
echo "obj.options[obj.options.length] = new Option('Copenhagen','12');\n";
echo "obj.options[obj.options.length] = new Option('Odense','13');\n";
break;
case "us":
echo "obj.options[obj.options.length] = new Option('Atlanta','21');\n";
echo "obj.options[obj.options.length] = new Option('Chicago','22');\n";
echo "obj.options[obj.options.length] = new Option('Denver','23');\n";
echo "obj.options[obj.options.length] = new Option('Los Angeles','24');\n";
echo "obj.options[obj.options.length] = new Option('New York','25');\n";
echo "obj.options[obj.options.length] = new Option('San Fransisco','26');\n";
echo "obj.options[obj.options.length] = new Option('Seattle','27');\n";
break;
}
}
?>
Copy the code and create the file getCities.php. Put it in the same folder as the main script
Comments
Post your comment
Please wait...



i write code above it don't work well.
i need this code very important
can you send it on my email
and a lot of thanks.
Wondering if there are any suggestions on getting this script to work in IE.
Thanks.
I ended up using it with the database http://worldcitiesdatabase.info.
They work great together.
I am getting an "unterminated string literal" error at the line "eval(ajax[index].response); // Executing the response from Ajax as Javascript code"
and this error is occurring in all browsers. Please help!
The codes are work very well. However, If I have another chain, it does not work.
Example: I select Building - Give the Floor list then If I select the Floor List to display the rooms in that floor - It does not work. Please help. Thanks again!
BV
Better, you help me a server side language in asp like getCities.asp.
thanks
function createCities(index)
{
var obj = document.getElementById('subcode1','subcode2')
eval(ajax[index].response); // Executing the response from Ajax as Javascript code
}
Is there anyway to specify color styling of individual options and Label group option?
plz help me...
You can check this adding one line:
alert(ajax[index].response);
In the function:
function createSubCategories(index)
{
alert(ajax[index].response);
var obj = document.getElementById('dhtmlgoodies_subcategory');
eval(ajax[index].response);
}
then you are going to see an alert with all the text than your are putting in the select box!!!
Try to find something like
echo "obj.options[obj.options.length] = new Option('Ber,'1');\n";
If you note, the name of the city doesn't end property!!! ('ber, '1') It must be ('ber', '1')
You Stupid ARAB!
i'm using your script with 3 selectboxes (works perfect) but i don't know what i have to change/add when i for example have this
1. Car = BMW
2. Model = 7er
3. Motor = 200PS
when i change select 1 to another car the 2nd select clears automaticly.. but not the 3rd selectbox! what can i do to clear the 3rd box??
thanks for helping
b