Papermashup

Subscribe


Tweets


"RT @bethgordon: .@Tesco Please rethink archaic 'Approved by Mums'. Pretty sure it's not just mothers who make childcare decisions. https://…"

@ashleyford 3 days ago

"Is there ever a time that @DFS doesn't have a sale on?"

@ashleyford 7 days ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Papermashup

Country drop down list with PHP function

238 countries and continents in a PHP function

AshleyAshley

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 could 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

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 10
  • SteeL
    Posted on

    SteeL SteeL

    Reply Author

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


  • Dev Guy
    Posted on

    Dev Guy Dev Guy

    Reply Author

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


  • Alexander
    Posted on

    Alexander Alexander

    Reply Author

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

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


  • Alexander
    Posted on

    Alexander Alexander

    Reply Author

    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


  • Daniel
    Posted on

    Daniel Daniel

    Reply Author

    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.


  • Łukasz Gręźlikowski
    Posted on

    Łukasz Gręźlikowski Łukasz Gręźlikowski

    Reply Author

    Is there South Sudan in this list? 😉


  • shox sko
    Posted on

    shox sko shox sko

    Reply Author

    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
    Posted on

    Heiko Heiko

    Reply Author

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


  • rmmoul
    Posted on

    rmmoul rmmoul

    Reply Author

    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
    Posted on

    Physicsmazz Physicsmazz

    Reply Author

    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