HTML/PHP Dynamic DropDown with countries

Posted on November 8, 2008, under PHP 

This is a snippet that creates a dynamic dropdown menu with countries. The select is created from the $countries array. You can set a country that will be selected automatically when the drop down is loaded. This can be done by editing the variable $default. You can specify the key from the array (i.e.: 220 for US) or the country name (i.e.: Argentina).

Dynamic DropDown (PHP)

config.php

<?php
/*
---------------
Countries List
---------------
*/

$countries = array('1' => 'Afghanistan',
'2' => 'Albania',
'3' => 'Algeria',
'4' => 'American Samoa',
'5' => 'Andorra',
'6' => 'Angola',
'7' => 'Anguilla',
'8' => 'Antarctica',
'9' => 'Antigua and Barbuda',
'10' => 'Argentina',
'11' => 'Armenia',
'12' => 'Aruba',
'13' => 'Australia',
'14' => 'Austria',
'15' => 'Azerbaidjan',
'16' => 'Bahamas',
'17' => 'Bahrain',
'18' => 'Bangladesh',
'19' => 'Barbados',
'20' => 'Belarus',
'21' => 'Belgium',
'22' => 'Belize',
'23' => 'Benin',
'24' => 'Bermuda',
'25' => 'Bhutan',
'26' => 'Bolivia',
'27' => 'Bosnia-Herzegovina',
'28' => 'Botswana',
'29' => 'Bouvet Island',
'30' => 'Brazil',
'31' => 'British Indian Ocean Territory',
'32' => 'Brunei Darussalam',
'33' => 'Bulgaria',
'34' => 'Burkina Faso',
'35' => 'Burundi',
'36' => 'Cambodia',
'37' => 'Cameroon',
'38' => 'Canada',
'39' => 'Cape Verde',
'40' => 'Cayman Islands',
'41' => 'Central African Republic',
'42' => 'Chad',
'43' => 'Chile',
'44' => 'China',
'45' => 'Christmas Island',
'46' => 'Cocos (Keeling) Islands',
'47' => 'Colombia',
'48' => 'Comoros',
'49' => 'Congo',
'50' => 'Cook Islands',
'51' => 'Costa Rica',
'52' => 'Croatia',
'53' => 'Cyprus',
'54' => 'Czech Republic',
'55' => 'Denmark',
'56' => 'Djibouti',
'57' => 'Dominica',
'58' => 'Dominican Republic',
'59' => 'East Timor',
'60' => 'Ecuador',
'61' => 'Egypt',
'62' => 'El Salvador',
'63' => 'Equatorial Guinea',
'64' => 'Eritrea',
'65' => 'Estonia',
'66' => 'Ethiopia',
'67' => 'Falkland Islands',
'68' => 'Faroe Islands',
'69' => 'Fiji',
'70' => 'Finland',
'71' => 'Former USSR',
'72' => 'France',
'73' => 'France (European Territory)',
'74' => 'French Guyana',
'75' => 'French Southern Territories',
'76' => 'Gabon',
'77' => 'Gambia',
'78' => 'Georgia',
'79' => 'Germany',
'80' => 'Ghana',
'81' => 'Gibraltar',
'82' => 'Greece',
'83' => 'Greenland',
'84' => 'Grenada',
'85' => 'Guadeloupe (French)',
'86' => 'Guam',
'87' => 'Guatemala',
'88' => 'Guinea',
'89' => 'Guinea Bissau',
'90' => 'Guyana',
'91' => 'Haiti',
'92' => 'Heard and McDonald Islands',
'93' => 'Honduras',
'94' => 'Hong Kong',
'95' => 'Hungary',
'96' => 'Iceland',
'97' => 'India',
'98' => 'Indonesia',
'99' => 'Iraq',
'100' => 'Ireland',
'101' => 'Israel',
'102' => 'Italy',
'103' => 'Ivory Coast',
'104' => 'Jamaica',
'105' => 'Japan',
'106' => 'Jordan',
'107' => 'Kazakhstan',
'108' => 'Kenya',
'109' => 'Kiribati',
'110' => 'Kuwait',
'111' => 'Kyrgyzstan',
'112' => 'Laos',
'113' => 'Latvia',
'114' => 'Lebanon',
'115' => 'Lesotho',
'116' => 'Liberia',
'117' => 'Libya',
'118' => 'Liechtenstein',
'119' => 'Lithuania',
'120' => 'Luxembourg',
'121' => 'Macau',
'122' => 'Macedonia',
'123' => 'Madagascar',
'124' => 'Malawi',
'125' => 'Malaysia',
'126' => 'Maldives',
'127' => 'Mali',
'128' => 'Malta',
'129' => 'Marshall Islands',
'130' => 'Martinique (French)',
'131' => 'Mauritania',
'132' => 'Mauritius',
'133' => 'Mayotte',
'134' => 'Mexico',
'135' => 'Micronesia',
'136' => 'Moldavia',
'137' => 'Monaco',
'138' => 'Mongolia',
'139' => 'Montserrat',
'140' => 'Morocco',
'141' => 'Mozambique',
'142' => 'Myanmar, Union of (Burma)',
'143' => 'Namibia',
'144' => 'Nauru',
'145' => 'Nepal',
'146' => 'Netherlands',
'147' => 'Netherlands Antilles',
'148' => 'Neutral Zone',
'149' => 'New Caledonia (French)',
'150' => 'New Zealand',
'151' => 'Nicaragua',
'152' => 'Niger',
'153' => 'Nigeria',
'154' => 'Niue',
'155' => 'Norfolk Island',
'156' => 'Northern Mariana Islands',
'157' => 'Norway',
'158' => 'Oman',
'159' => 'Pakistan',
'160' => 'Palau',
'161' => 'Panama',
'162' => 'Papua New Guinea',
'163' => 'Paraguay',







'164' => 'Peru',
'165' => 'Philippines',
'166' => 'Pitcairn Island',
'167' => 'Poland',
'168' => 'Polynesia (French)',
'169' => 'Portugal',
'170' => 'Qatar',
'171' => 'Reunion (French)',
'172' => 'Romania',
'173' => 'Russian Federation',
'174' => 'Rwanda',
'175' => 'S. Georgia & S. Sandwich Islands',
'176' => 'Saint Helena',
'177' => 'Saint Kitts & Nevis Anguilla',
'178' => 'Saint Lucia',
'179' => 'Saint Pierre and Miquelon',
'180' => 'Saint Tome and Principe',
'181' => 'Saint Vincent & Grenadines',
'182' => 'Samoa',
'183' => 'San Marino',
'184' => 'Saudi Arabia',
'185' => 'Senegal',
'186' => 'Seychelles',
'187' => 'Sierra Leone',
'188' => 'Singapore',
'189' => 'Slovakia',
'190' => 'Slovenia',
'191' => 'Solomon Islands',
'192' => 'Somalia',
'193' => 'South Africa',
'194' => 'South Korea',
'195' => 'Spain',
'196' => 'Sri Lanka',
'197' => 'Suriname',
'198' => 'Svalbard and Jan Mayen Islands',
'199' => 'Swaziland',
'200' => 'Sweden',
'201' => 'Switzerland',
'202' => 'Tadjikistan',
'203' => 'Taiwan',
'204' => 'Tanzania',
'205' => 'Thailand',
'206' => 'Togo',
'207' => 'Tokelau',
'208' => 'Tonga',
'209' => 'Trinidad and Tobago',
'210' => 'Tunisia',
'211' => 'Turkey',
'212' => 'Turkmenistan',
'213' => 'Turks and Caicos Islands',
'214' => 'Tuvalu',
'215' => 'Uganda',
'216' => 'UK',
'217' => 'Ukraine',
'218' => 'United Arab Emirates',
'219' => 'Uruguay',
'220' => 'US',
'221' => 'USA Minor Outlying Islands',
'222' => 'Uzbekistan',
'223' => 'Vanuatu',
'224' => 'Vatican City',
'225' => 'Venezuela',
'226' => 'Vietnam',
'227' => 'Virgin Islands (British)',
'228' => 'Virgin Islands (USA)',
'229' => 'Wallis and Futuna Islands',
'230' => 'Western Sahara',
'231' => 'Yemen',
'232' => 'Yugoslavia',
'233' => 'Zaire',
'234' => 'Zambia',
'235' => 'Zimbabwe');

/* 
Searches the array for a given value (insensitive case) and returns the corresponding key if successful
*/

function array_isearch($value, $array)
{
   while (list($key, $val) = each($array))
   {
      $val = strtolower($val);
	  $value = strtolower($value);

      if($val == $value) return $key;
   }
   return false;
}
?>

index.php

<?php
include 'config.php';

/* Already selected? (Choose either a number (key array) or a country name) */

$default = 220; // or you could use: $default = 'us'; // case insensitive

    if(is_int($default))
	{
		$selected_key = $default;
	}
	else
	{
		$selected_key = array_isearch($default, $countries);
	}

$select = '<select name="country">'."n";

foreach($countries as $key => $value)
{
	if($selected_key)
	{
		if($key == $selected_key)
		{
		$selected = 'selected';
		}
		else
		{
		$selected = '';
		}
	}
	else
	{
	$selected = '';
	}

$select .= '<option '.$selected.' value="'.$key.'">'.$value.'</option>'."n";
}

$select .= '</select>';
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>Countries DropDown Selector</TITLE>
 </HEAD>
 <BODY>

  <?php
  echo $select;
  ?>
 </BODY>
</HTML>

DropDown in HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>Countries Drop Down Selector</TITLE>
 </HEAD>

 <BODY>
  <select name="country">
<option  value="1">Afghanistan</option>
<option  value="2">Albania</option>

<option  value="3">Algeria</option>
<option  value="4">American Samoa</option>
<option  value="5">Andorra</option>
<option  value="6">Angola</option>

<option  value="7">Anguilla</option>
<option  value="8">Antarctica</option>
<option  value="9">Antigua and Barbuda</option>
<option  value="10">Argentina</option>

<option  value="11">Armenia</option>
<option  value="12">Aruba</option>
<option  value="13">Australia</option>
<option  value="14">Austria</option>

<option  value="15">Azerbaidjan</option>
<option  value="16">Bahamas</option>
<option  value="17">Bahrain</option>
<option  value="18">Bangladesh</option>

<option  value="19">Barbados</option>
<option  value="20">Belarus</option>
<option  value="21">Belgium</option>
<option  value="22">Belize</option>

<option  value="23">Benin</option>
<option  value="24">Bermuda</option>
<option  value="25">Bhutan</option>
<option  value="26">Bolivia</option>

<option  value="27">Bosnia-Herzegovina</option>
<option  value="28">Botswana</option>
<option  value="29">Bouvet Island</option>
<option  value="30">Brazil</option>

<option  value="31">British Indian Ocean Territory</option>
<option  value="32">Brunei Darussalam</option>
<option  value="33">Bulgaria</option>
<option  value="34">Burkina Faso</option>

<option  value="35">Burundi</option>
<option  value="36">Cambodia</option>
<option  value="37">Cameroon</option>
<option  value="38">Canada</option>

<option  value="39">Cape Verde</option>
<option  value="40">Cayman Islands</option>
<option  value="41">Central African Republic</option>
<option  value="42">Chad</option>

<option  value="43">Chile</option>
<option  value="44">China</option>
<option  value="45">Christmas Island</option>
<option  value="46">Cocos (Keeling) Islands</option>

<option  value="47">Colombia</option>
<option  value="48">Comoros</option>
<option  value="49">Congo</option>
<option  value="50">Cook Islands</option>

<option  value="51">Costa Rica</option>
<option  value="52">Croatia</option>
<option  value="53">Cyprus</option>
<option  value="54">Czech Republic</option>

<option  value="55">Denmark</option>
<option  value="56">Djibouti</option>
<option  value="57">Dominica</option>
<option  value="58">Dominican Republic</option>

<option  value="59">East Timor</option>
<option  value="60">Ecuador</option>
<option  value="61">Egypt</option>
<option  value="62">El Salvador</option>

<option  value="63">Equatorial Guinea</option>
<option  value="64">Eritrea</option>
<option  value="65">Estonia</option>
<option  value="66">Ethiopia</option>

<option  value="67">Falkland Islands</option>
<option  value="68">Faroe Islands</option>
<option  value="69">Fiji</option>
<option  value="70">Finland</option>

<option  value="71">Former USSR</option>
<option  value="72">France</option>
<option  value="73">France (European Territory)</option>
<option  value="74">French Guyana</option>

<option  value="75">French Southern Territories</option>
<option  value="76">Gabon</option>
<option  value="77">Gambia</option>
<option  value="78">Georgia</option>

<option  value="79">Germany</option>
<option  value="80">Ghana</option>
<option  value="81">Gibraltar</option>
<option  value="82">Greece</option>

<option  value="83">Greenland</option>
<option  value="84">Grenada</option>
<option  value="85">Guadeloupe (French)</option>
<option  value="86">Guam</option>

<option  value="87">Guatemala</option>
<option  value="88">Guinea</option>
<option  value="89">Guinea Bissau</option>
<option  value="90">Guyana</option>

<option  value="91">Haiti</option>
<option  value="92">Heard and McDonald Islands</option>
<option  value="93">Honduras</option>
<option  value="94">Hong Kong</option>

<option  value="95">Hungary</option>
<option  value="96">Iceland</option>
<option  value="97">India</option>
<option  value="98">Indonesia</option>

<option  value="99">Iraq</option>
<option  value="100">Ireland</option>
<option  value="101">Israel</option>
<option  value="102">Italy</option>

<option  value="103">Ivory Coast</option>
<option  value="104">Jamaica</option>
<option  value="105">Japan</option>
<option  value="106">Jordan</option>

<option  value="107">Kazakhstan</option>
<option  value="108">Kenya</option>
<option  value="109">Kiribati</option>
<option  value="110">Kuwait</option>

<option  value="111">Kyrgyzstan</option>
<option  value="112">Laos</option>
<option  value="113">Latvia</option>
<option  value="114">Lebanon</option>

<option  value="115">Lesotho</option>
<option  value="116">Liberia</option>
<option  value="117">Libya</option>
<option  value="118">Liechtenstein</option>

<option  value="119">Lithuania</option>
<option  value="120">Luxembourg</option>
<option  value="121">Macau</option>
<option  value="122">Macedonia</option>

<option  value="123">Madagascar</option>
<option  value="124">Malawi</option>
<option  value="125">Malaysia</option>
<option  value="126">Maldives</option>

<option  value="127">Mali</option>
<option  value="128">Malta</option>
<option  value="129">Marshall Islands</option>
<option  value="130">Martinique (French)</option>

<option  value="131">Mauritania</option>
<option  value="132">Mauritius</option>
<option  value="133">Mayotte</option>
<option  value="134">Mexico</option>

<option  value="135">Micronesia</option>
<option  value="136">Moldavia</option>
<option  value="137">Monaco</option>
<option  value="138">Mongolia</option>

<option  value="139">Montserrat</option>
<option  value="140">Morocco</option>
<option  value="141">Mozambique</option>
<option  value="142">Myanmar, Union of (Burma)</option>

<option  value="143">Namibia</option>
<option  value="144">Nauru</option>
<option  value="145">Nepal</option>
<option  value="146">Netherlands</option>

<option  value="147">Netherlands Antilles</option>
<option  value="148">Neutral Zone</option>
<option  value="149">New Caledonia (French)</option>
<option  value="150">New Zealand</option>

<option  value="151">Nicaragua</option>
<option  value="152">Niger</option>
<option  value="153">Nigeria</option>
<option  value="154">Niue</option>

<option  value="155">Norfolk Island</option>
<option  value="156">Northern Mariana Islands</option>
<option  value="157">Norway</option>
<option  value="158">Oman</option>

<option  value="159">Pakistan</option>
<option  value="160">Palau</option>
<option  value="161">Panama</option>
<option  value="162">Papua New Guinea</option>

<option  value="163">Paraguay</option>
<option  value="164">Peru</option>
<option  value="165">Philippines</option>
<option  value="166">Pitcairn Island</option>

<option  value="167">Poland</option>
<option  value="168">Polynesia (French)</option>
<option  value="169">Portugal</option>
<option  value="170">Qatar</option>

<option  value="171">Reunion (French)</option>
<option  value="172">Romania</option>
<option  value="173">Russian Federation</option>
<option  value="174">Rwanda</option>

<option  value="175">S. Georgia & S. Sandwich Islands</option>
<option  value="176">Saint Helena</option>
<option  value="177">Saint Kitts & Nevis Anguilla</option>

<option  value="178">Saint Lucia</option>
<option  value="179">Saint Pierre and Miquelon</option>
<option  value="180">Saint Tome and Principe</option>
<option  value="181">Saint Vincent & Grenadines</option>

<option  value="182">Samoa</option>
<option  value="183">San Marino</option>
<option  value="184">Saudi Arabia</option>
<option  value="185">Senegal</option>

<option  value="186">Seychelles</option>
<option  value="187">Sierra Leone</option>
<option  value="188">Singapore</option>
<option  value="189">Slovakia</option>

<option  value="190">Slovenia</option>
<option  value="191">Solomon Islands</option>
<option  value="192">Somalia</option>
<option  value="193">South Africa</option>

<option  value="194">South Korea</option>
<option  value="195">Spain</option>
<option  value="196">Sri Lanka</option>
<option  value="197">Suriname</option>

<option  value="198">Svalbard and Jan Mayen Islands</option>
<option  value="199">Swaziland</option>
<option  value="200">Sweden</option>
<option  value="201">Switzerland</option>

<option  value="202">Tadjikistan</option>
<option  value="203">Taiwan</option>
<option  value="204">Tanzania</option>
<option  value="205">Thailand</option>

<option  value="206">Togo</option>
<option  value="207">Tokelau</option>
<option  value="208">Tonga</option>
<option  value="209">Trinidad and Tobago</option>

<option  value="210">Tunisia</option>
<option  value="211">Turkey</option>
<option  value="212">Turkmenistan</option>
<option  value="213">Turks and Caicos Islands</option>

<option  value="214">Tuvalu</option>
<option  value="215">Uganda</option>
<option  value="216">UK</option>
<option  value="217">Ukraine</option>

<option  value="218">United Arab Emirates</option>
<option  value="219">Uruguay</option>
<option  value="220">US</option>
<option  value="221">USA Minor Outlying Islands</option>

<option  value="222">Uzbekistan</option>
<option  value="223">Vanuatu</option>
<option  value="224">Vatican City</option>
<option  value="225">Venezuela</option>

<option  value="226">Vietnam</option>
<option  value="227">Virgin Islands (British)</option>
<option  value="228">Virgin Islands (USA)</option>
<option  value="229">Wallis and Futuna Islands</option>

<option  value="230">Western Sahara</option>
<option  value="231">Yemen</option>
<option  value="232">Yugoslavia</option>
<option  value="233">Zaire</option>

<option  value="234">Zambia</option>
<option  value="235">Zimbabwe</option>
</select> 
</BODY>
</HTML>

Comment via Facebook

comments

4 Replies to "HTML/PHP Dynamic DropDown with countries"

  1. […] here:  Dynamic DropDown with countries Tags: argentina, central-african, dynamic-drop, html, islands, library, option-value, programming, […]

  2. […] Dynamic JS DropDown with countries | PHP Library… value=”179″>Saint Pierre and Miquelon Saint Tome and Principe Saint Vincent & Grenadines Samoa San Marino … […]

  3. wow… excellent menu…. can this be done with python? :)

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)

POSTING RULES:

  • The comment must be relevant with the topic of the post.
  • Only comments with real email addresses will get approved. So, emails like 'abc@domain.com' will not be accepted.
  • Do not post the same message in multiple articles through the site.
  • Do not post advertisements, junk mail or pyramid schemes.
  • In case you post a link to another site, please explain briefly where the link goes as a courtesy to other users.
  • Do not post comments such as: "Thank you", "Awesome", "Nice tutorial", "Merci", etc.