Ask Felgall Home page
Your host:
Stephen Chapman
Stephen Chapman

Country Selection Drop Down Box

When you are creating a form, you often want the person filling out the form to provide an address. Given the international nature of the internet, one thing that you need to capture as part of any address is the Country. The countries in the world are relatively static (barring wars and successions) so a drop down list where the person can select their country rather than typing it is one option to consider. Unfortunately there are quite a lot of countries in the world and finding a relatively complete list may take some time let alone the time required to convert that list into a drop down selection box.

Never fear because I am about to save you the trouble of doing any of that. The following selection box allows you to select one of 235 different countries which is as close as I can get to including all of the countries of the world. You are welcome to use this on any non-commercial web site that you may have. All that I ask is that you don't change any of the code. If you have a commercial website you may still use the script (subject to the same conditions) in return for a donation toward the cost of my maintaining this site.

The code to list these 235 different countries in a drop down selection list has been converted to an external javascript called country.js by enclosing the code in document.write(' '); statements. This means that you can easily incorporate into your form by specifying the following code (to reference the copy that you have made of this external javascript) in your form where you want the country drop down box to appear:

<script type="text/javascript" src="country.js"></script>

I have set the script to default to Australia but you may wish to amend your copy of the script to default to a different country. I have set up the code to make it easy to set the default country to any of Australia, Canada, France, Germany, New Zealand, United Kingdom, or United States. Alternatively if you don't want to default to a particular country there is also an option to add a blank entry at the top of the list and default to that. To change the default you just move the selected="selected" from the aus variable at the top of the script to the variable for the country that you want as the default. If you want a different country as the default just let me know and I will provide you with an amended copy of the script.

The supplied code uses the name country for this selection box so that is the field name that you will want to reference in any subsequent processing of the field. For example if you include it in a form to be mailed to you via a form2mail script and the form is sent with the default country value then your email should contain something like country = Australia. This name is assigned to the variable fldname to make it easy to change the name of the field if you need to call the field something different.

This code is provided free for your use provided that you keep the code intact (apart from changing the default country and field name using the variables provided).

Related Articles

go to top

FaceBook Follow
Twitter Follow
Donate
Copyright © Felgall Pty Ltd