Creating Smarter Forms Using Geolocation

closeThis post was published 5 years 10 months 7 days ago, which is pretty much forever ago in internet time. As such, some of what's discussed here may no longer work as described.
Geolocation can be used to pre-select a country from a drop-down menu

Every country in the world, and the U.S. is near the bottom of the list. We could use geolocation to pre-select it for U.S. users.

I was filling out a form online recently, and one of the questions was “country.” As I scrolled through a seemingly endless drop-down list—one that contained every country in the world—I lamented aloud that “United States” wasn’t right at the top. That’s when it hit me: We have reasonably accurate IP-based geolocation services available, so why not attempt to figure out where the user is, and then use our best-guess to pre-select that item in the list? A quick proof-of-concept shows that there’s no reason not to.

The What and How of Geolocation

I’m sure you’ve seen those (probably scam) ads that say some mom made a miracle discovery (and doctors/dentists/insurance companies/et al hate her and her 1 weird trick). They often say where this mom supposedly lives, and she always just so happens to live in the same town that you’re viewing the ad from. Amazing!

On my personal blog, I’ve talked about how the town changes depending on your location. This works thanks to IP-based geolocation. It’s easy for me (or any developer) to grab your IP address:

I can then cross-check that against a database to make a “best guess” of where you are:

Oops, I couldn't find anything!

Accuracy of Geolocation Services

If the example above guessed the wrong location for you, don’t be too surprised; geolocation is far from an exact science.

I was working at a FOX affiliate station during the time when all the networks were trying to figure out how best to stream their shows to viewers. In the television industry, viewers live in regions called DMAs, and the network execs at FOX wanted to make sure users would watch shows streamed from the station representing their DMA (for a number of reasons, but mostly so they could sell local advertising). The basic idea was, if you lived in Georgia, but visited the website for a FOX station in Florida and tried to stream a show, you should automatically be sent to the FOX website for the Georgia station. The company that set all of this up was the now-defunct FOX Interactive Media (or FIM for short).

I suspected that FIM was using geolocation to accomplish this, so I asked one of the representatives how it worked. He confirmed my suspicion. I then pointed out that geolocation isn’t always perfect. I live in Washington state, but on more than one occasion, geolocation has put me in California. For a long time, all AOL dialup customers were geolocated to Virginia, because that’s where AOL’s headquarters were at the time. The FIM representative assured me that such issues had all been ironed out. I remained skeptical. In the end I was proved right when some FOX users in New York were redirected to the website for a FOX station in Kansas. Oops.

The fact of the matter is that IP-based geolocation is not perfect. There are a number of issues that can cause problems, including inaccurate reporting of IP location data (see this article for more information about the collection of IP location data).

Multiple IP-based geolocation databases exist, and every database is different, but in general, accuracy breaks down along these lines:

Country: 98%+
U.S. States: 80%+
U.S. Cities: 60%+

The takeaway from this is: The more specific you want to get, the less accurate the data will be.

Building User-Friendly Forms

As you can see in this proof-of-concept, grabbing a user’s geolocation data is easy, and using that information to automatically pre-populate form fields is dead-simple.

For this demo I am sending a cURL request to to get the user’s geolocation info. The site will return the information in a few formats, and JSON was the obvious choice for me.

To create the drop-downs, I started with arrays that looked like this:

$states = array(
  'AL' => 'Alabama',
  'AK' => 'Alaska',
  'WI' => 'Wisconsin',
  'WY' => 'Wyoming'

I can then build each drop-down with a foreach() and compare the key to the region_code in the geolocation data.

foreach($states as $k => $v)
	$selected = '';
	if(!empty($geo_arr->latitude) && ($k == $geo_arr->region_code))
		$selected = ' selected="selected"';
	echo '<option value="'.$k.'"'.$selected.'>'.$v.'</option>';

Keeping the accuracy factor in mind, I built-in a check to make sure we actually received data. It’s probably not the best check in the world, but this was just a quick-n-dirty example to prove that it could be done. I also used the city information I received to pre-populate the city field in the form.

What if it guesses wrong? For the city field, that would be a bit embarrassing and perhaps a little confusing for the user, but for country and state, the user is no worse off than if geolocation data wasn’t used.

Worth the Effort?

You might be thinking to yourself, “all this effort just to fill in a couple of form fields?” It may not seem like much, but honestly, it’s fast and simple to use. In fact, you would do well to write a function to handle all of this for you.

If additional HTTP requests or slower page load times are your concern, perhaps I can allay those fears by pointing out that you don’t need to use an external source to get geolocation data. The source that relies on is freely available on GitHub. It’s as simple as making an additional database call. Plus, you don’t need to do this on every page, just your shopping cart’s checkout page (or wherever you’re asking a user to supply their address).

Thoughts About Geolocation on Mobile Devices

Mobile is a double-edged sword with this one. On the one hand, it would be a huge boon to mobile users not to have to fill out their city, state, and country. Filling out forms on mobile sites is already a pain, so anything we can do to make it easier is good news for the user.

But—and this is a big but—the geolocation data will probably be for where the user currently is, which may not be their home city, state, or even country. If I’m doing a little online shopping while on vacation, it could be frustrating for the form to automatically fill in the wrong information. With that said, as of June 2012, 31% of Americans who access the internet from a mobile device say they rarely or never use a desktop or laptop computer.

In the case of mobile, I would probably not auto-populate the city field, but I would fill state and country based on geolocation data. Another option is to include the option to use the phone’s current location to populate the form fields.

2 comments to Creating Smarter Forms Using Geolocation

  • Danti  says:

    Hi, I have a job search portal at and it has an option to show jobs for the country selected. I have a simple form with 'Select' and 'Option' tag which does this. Can you help me out with the auto country selection thing ?
    I'll be really grateful. PS Im a total noob.

  • sproutpeds  says:

    I am wondering about geolocating visitors and I have been reading different blogs, etc. Mobile phones are so much more accurate with this than web browsers. Still I think that it would be useful to be able to identify a site visitors location and then present them with different options based on that location. Great article. Thanks.

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>