Country dialing code select drop menu
A recent update to Server Density, our server monitoring service, replaces a free form text field with a drop menu when selecting your country dialing code. This reduces the possibility for error but was a real pain to implement because I couldn’t find a list of countries in drop menu form.
As such, I had to make it manually. And to save anyone else the time, here is the HTML and PHP code used to handle the menu.
Country dialing code HTML
Some countries have the same country dialing code (e.g. Canada and USA) so using that as the option value does not work. As such, I simply created a PHP array of all the countries – view on Github. This creates the HTML select drop menu – view on Github.
PHP processing
As the select drop menu uses a simple incrementing key, this needs to be translated to the correct value when the form is submitted. This is done using the previous array and a switch block – view on Github.



Thanks for the big array, that would have been a pain to make myself.
I made it a bit easier to use in webapps that don’t like raw html too much:
https://gist.github.com/260620/824dd5dbac97eeeb2dc8d79cb86573566768f21d
line 195 of newarray.php,
‘code’ => Zaire243,
should change to,
‘code’ => 243,
May be a bit late for this post, but you should have the array in format:
$countries = array(
1 => array(‘country_code’ => 64, ‘country_name’ => ‘New Zealand’),
2 => array(‘country_code’ => 1, ‘country_name’ => ‘United States’)
);
etc.
And then do
foreach($countries as $index => $country_data) {
echo “{$country_data['country_name']} ({$country_data['country_code']})”;
}
and then in processing do..
$country = $countries[$formData['countryCode']];
$country_code = $country['country_code'];
A little simpler than redoing it 3 times.
Cheers
Like this https://gist.github.com/260620/824dd5dbac97eeeb2dc8d79cb86573566768f21d ?