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.
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
);
}
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>
Thanks for sharing the list, I wish the result City wud get automatic too.. the SWITCH CASE…
This is a good example, although I would prefer using an array to populate the list.
(continued) To auto-select the visitors country, use this code in the getScript load event handler:
$(‘#countrySelect’).val(geoip_country_code());
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
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.
Is there South Sudan in this list?
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
If you want to know the country code of a country you have a problem.
Physicsmazz’s solution is much better!
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.
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
Designer and web developer, Co-founder and Technical Director at Harkable.com London. Previously I worked at InMobi, Spotify and MySpace. Interests include photography and making short videos. Also an avid F1 fan. I also run Mega Infographics for your daily dose of the best infographics.
Follow us on Twitter and get in-stream updates.
Subscribe to all the Papermashup tutorials and articles straight to your RSS reader.
Sign up and get all the Papermashup tutorials straight to your inbox.
10 discussions around Country drop down list with php function