Country drop down list with php function

19 November 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> 

Sumber : Jasa Pembuatan Website dan Sistem Informasi http://klatenweb.com
Selengkapnya : http://klatenweb.com/read/2011/11/19/739/Country-drop-down-list-with-php-function.html