Country dialing code select drop menu

December 20, 2009
by David Mytton

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.

One Response leave one →
  1. December 20, 2009

    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

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS