Massive AJAX Country/Area Drop-down List
I recently came across a useful post by a fellow South African blogger. Mike had compiled an XML list of almost all countries and their associated areas. Often I have had to produce an county/area option on registration forms, but have struggled to find a complete list. I took mikes XML list, converted it to CSV and imported to it to a MySQL database. I then found a fairly simple AJAX expanding selection script and connected it to the data in the database. The result is a database driven AJAX selection tool which re-generates the list of areas based on the country the user selects.

>> Demo
>> Download Source
This script was adapted from the following sources:
The AJAX form script:
http://www.skillfusion.com/articles/ajaxDropdown.php
XML country/area list:
http://www.darwinshome.za.net/blog/2008/02/08/country-selection-list-with-sate-and-province/











[...] Live demo | Download page [...]
It seems unnecessary to pull the info from a database. If you struggle with parsing XML, you could just put it into a php file, in a array, which would be easier, faster and less of a load than pulling the results from a database each time.
Hi Ethan. If I understand your correctly, you suggest to not use XML (In turn AJAX) and simply hard code all areas in a php file. Problem is there are 4395 areas. The script only draws the necessary areas from the DB(Max 50) which should require considerably less resources than hard coding the data.
Keeping the data in the database is also a good way to ensure data integrity on distributed-localized systems.
You are missing Serbia on your list (and you can select areas: Vojvodina and Kosovo).
Thanks
[...] on a few other sources. NoBoxMedia has packaged together a zip file, free to download, which has dynamic drop down menus with ajax for countries and areas. Here is an example of what I am referring [...]
You could still use an XML file, I just suggested it because you suggested using a database over flat files. On a small scale it wont make a difference but when you scale up and are trying to minimize unnecessary queries. You could split the files up, it would take a little time but you could make it so when you select a country, it associates a file with that county, you grab the data from that file, parse it, viola, you have your results. Its essentially doing the same thing as pulling it from the DB, just will require a little parsing of the data.
[...] Massive AJAX Country/Area Drop-down List By Robin I then found a fairly simple AJAX expanding selection script and connected it to the data in the database. The result is a database driven AJAX selection tool which re-generates the list of areas based on the country the user selects. … Nobox Media - http://www.noboxmedia.com [...]
Is there any easy way to make every second cell of the dropdown background color to differ? So it is more easy to distinguish?! One white, second black, 3d white, 4th black and so on. Is there such a DropDown + CSS + Dhtml / Ajax? script?
[...] 28)Massive AJAX Country/Area Drop-down List- A database driven AJAX selection tool which re-generates the list of areas based on the country the user selects. [...]
[...] 28)Massive AJAX Country/Area Drop-down List- A database driven AJAX selection tool which re-generates the list of areas based on the country the user selects. [...]
@omBRE - That is my fault, I will confirm and correct my XML file and add Serbia to the list.
@Ethan - I have used the XML directly in a small script on my page: here
Although running it from a database is still a great way to do it, as well as maintain control of the data on your server.
@Robin: in case you missed my reply on my site. GREAT JOB. I love what you did here.
How can I include the MassivAJAX Country/Area Drop-down List into the Joomla CB?
Hi Ana. Not too sure. I do not think it would be too difficult. I will ask one of my colleagues to have a look at it and get back to you, he is far more proficient at Joomla.
Hi admin. Thanks for your reply. Is there still no idea how i can include it into a plugin of the Joomla CB?
You’re missing the capital of New Zealand, which is Wellington.
@David
As New Zealand doesn’t truly have provinces or States like other countries I see why I missed that. Sorry about that.
I will correct it on the original XML list shortly
first of all thanks for the code. i’ve been looking for this for long every body tells something but no code and no explanation. ı want to use this dropdowns as database driven script. I want them to pull data from the database and post the datas to an other form as url parameter.Any help would be very helpfull thans from now.
I can’t speak for all of the countries listed, but for England, the Areas are a mixture of counties (like states or provinces) and some city boroughs such as London’s Camden. There is no logic to this. Only the counties should be listed, of which London is one.
can you please explain how to implement this on blogger….
http://free-downloadz.blogspot.com
got it to work, but not getting the state/area value. All I’m getting is numbers. and BTW:
for default to USA:
function createStateList()
{
global $cdb;
$sql=”SELECT * FROM country_areas.list WHERE country=’USA’ ORDER BY area”;
$res=$cdb->Query($sql);
while ($row=$cdb->GetRow($res))
{
echo “$row[area]“;
}
return;
}
Then, in your code:
$sql=”SELECT * from country_areas.list group by country”;
$rs=$cdb->Query($sql);
while ($row=$cdb->GetRow($rs))
{
$selected= ($row[country]==’USA’) ? ’selected’ : ” ;
?>
” >