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){
	
	switch($country){
		
		case 'AF':
		$name = 'Afghanistan';
		break;
	
	        case 'AL':
		$name = 'Albania';
		break;	
	
	        case 'DZ':
		$name = 'Algeria';
		break....	
	
	  } 
		
	return array(
		
		"name" => $name
		
		);


		}

The HTML

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


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

download



More tutorials from Papermashup
  • http://ajwebdesigner.in SteeL

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

  • http://www.vivitech.co.uk/ Dev Guy

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

  • http://www.oldenburgs.org/ Alexander

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

    $(‘#countrySelect’).val(geoip_country_code());

  • http://www.oldenburgs.org/ Alexander

    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: http://www.maxmind.com/app/javascript_city

  • http://www.shareyourride.net/ Daniel

    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.

  • http://www.grezlikowski.pl Łukasz Gręźlikowski

    Is there South Sudan in this list? ;)

  • http://www.nikeshox.dk shox sko

    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

  • Heiko

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

  • rmmoul

    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.

  • Physicsmazz

    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