John Rainsford

Constructing a Country List in PHP

This article was posted on Friday, 16th September 2011

There will come a time when you, as a web designer, will have to construct a HTML form with a place to enter the visitor's country of residence in. Now, I think, that an important rule of forms is to limit the amount of information that a visitor has to enter manually, to avoid mistakes and multiple variations of the same answer(s).

A classic example is when looking for the gender of a person- having a drop-down select menu with option 'female' and option 'male' avoids any variations in spelling (case of text or language) as well as actual spelling errors.

On a simple mailing form, this usually doesn't cause any issues, as a human will probably be interpreting the answers, but if you're entering the data in to a database, it'll make life very awkward trying to sort by gender or in the case of this blog post, country of residence.

I've been working on a booking form where the country of residence is important, and previously I had a long list of countries formatted like <option value="country name">Country Name</option> which I inserted whenever needed. However, once I needed to have some if/else statements in place to re-select the visitor-selected country, when the form bounced back with errors (for example, missing fields or invalid email address), I had to rethink this strategy.

Disclaimer

Before I go any further, let me state that I am not a PHP expert but I think this is fairly simple PHP that shouldn't cause any problems.

I started off by creating a PHP include file with an array of countries in it. I have zipped this file and you can download it here to take a look. Then, whenever I need to include a country list on a form, I simply include the country list file before running through a simple loop of the array. Here is an example of how I would use it-

<label for="country">Country:</label>

<select name="country" id="country">

<?php include ('countrylist.inc.php'); //include the country list file

//start loop

foreach ($countrylist as $row){ ?>

<option value="<?php echo $row; ?>"

<?php

// Enter your if/else statement here to set 'selected="selected"' if throwing errors

?>

>

<?php echo $row; ?>

</option>

<?php } ?>

</select>

Easy!

Pretty basic PHP 101 stuff and this blog post is definitely not aimed at back-end coders/developers. Forms, whether linked to database data or a simple mailing form, is one area that is often handled by front-end coders and designers, and there is no need to languish doing incredibly repetitive stuff with a couple of hundred country entries when an array, looped through with PHP (or similar language) means that you only have to worry about a single <option> than hundreds of them. 

I hope this helps, the country list can be downloaded here. I'm offering this as-is with no promise of support of its use. Sorry, I won't be able to provide tech support for it, but might I suggest this book (under a shameless Amazon Associates link) PHP Solutions by David Powers. It's a great book and will run through PHP from the ground up.

If you have any queries or comments, please use the contact form.

There are more articles in the Archive

Contents © 2008 - 2017 John Rainsford. All rights reserved. Hosted by Blacknight.