Use your left/right keys to browse tutorials
Country drop down list with php function

Country drop down list with php function

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on August 18, 2011

I had a project recently that required the user to select their country from a drop down list and store the data in a database. It was such a long process setting up the PHP switch statement that I thought someone might find it useful for a project. The code below is a snapshot of the switch statement I’ve setup. The download at the bottom includes 238 countries and continents. You just need to store the country code in the database then call the function below to return the country name.

Code snapshot

Below is a snapshop of the PHP switch statement. Download the full code of the tutorial here. Upload the PHP file included and see the demos.

function user_country($country){
		case 'AF':
		$name = 'Afghanistan';
	        case 'AL':
		$name = 'Albania';
	        case 'DZ':
		$name = 'Algeria';
	return array(
		"name" => $name



Again this is a snapshot of the select drop down form.

        <select name="country"> 
          <option value="AF">Afghanistan</option> 
          <option value="AL">Albania</option> 
          <option value="DZ">Algeria</option> 


More tutorials from Papermashup
10 discussions around Country drop down list with php function
  1. SteeL says:

    Thanks for sharing the list, I wish the result City wud get automatic too.. the SWITCH CASE…

  2. Dev Guy says:

    This is a good example, although I would prefer using an array to populate the list.

  3. Alexander says:

    (continued) To auto-select the visitors country, use this code in the getScript load event handler:


  4. Alexander says:

    For a project I’m working on I use MaxMind’s (free) Geo IP API to pre-fill the country and city. Just load the javascript ($.getScript in jQuery, and make sure caching is enabled) and set the value once loaded. For those interested, see:

  5. Daniel says:

    I have to agree with the previous posters; your code is … sub-optimal.
    Use arrays or poll Country+name from your database, then populate your select-option field inside a loop, like this:

    $counties = array(‘AF’=>’Afghanistan’, ‘AL’=>’Albania’);
    foreach($countries as $code->$country)
    echo ”.$country.”;

    Another thing i noticed on websites with international audiences: Always have “United States” either pre-selected (unless you do proper ip-country detection) or on top of your list. For some reason most Americans never change the country-field and leave it at “Afghanistan” – Visitors from Canada, South America or Europe don’t show this behavior.

  6. Is there South Sudan in this list? ;)

  7. shox sko says:

    this way you can loop through the array to populate the select. You can also get the full name by doing something like echo $countryArr['al']; //returns Albania

  8. Heiko says:

    If you want to know the country code of a country you have a problem.
    Physicsmazz’s solution is much better!

  9. rmmoul says:

    I had to do this awhile back myself, and rather than storing the country code, i got a list of comma separated countries, read them into an array and wrote a quick script to print them to my browser with the code that i needed printed around them. I ended up using numbers to store them in the db and reference them later. This let me copy and paste the code right from my browser and into the page i was working on.

    my code (javascript) did something like:

    document.write(“case \’”+int+”\’ : { $country = \’”+country[int]+”+\’; break; }”);

    I write quick scripts like that for all sorts of long lists, forms when needed. I’ll write a script that enumerates all of the id’s of every element in my document and I’ll have it use those to grab the values from $_POST and drop them into corresponding php variables for server side validation, or db entries and such. This method has saved me a ton of time.

  10. Physicsmazz says:

    In the past I’ve done this using an array. Just create an array of the countries: array(‘AF’=>’Afghanistan’, ‘AL’=>’Albania’)…this way you can loop through the array to populate the select. You can also get the full name by doing something like echo $countryArr['al']; //returns Albania

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.

What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.