Digital(Analog)

Don’t ever…

July 14, 2009 8:58 am

make me type a city and state in a form.

An address form from LinkedIn

An address form from LinkedIn

If you’re in the US (and I suppose this is true for any country with a robust postal code system) there is a unique mapping of postal code to city.

Writing a web service to turn the postal code into the city and state would be a triviality. I wrote one a few weeks ago just as a test.

There are free and pay lists of zip code -> city mappings which can be loaded in to a DB one one command, and the getting the data is a few lines of PHP. (yeah, I know this isn’t production code, its just a basic example)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
$con = mysql_connect("xxx","xxx","xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("zipcodes", $con);
 
$zipcode = $_GET["zipcode"];
 
$result = mysql_query("SELECT city, state FROM codes where code='" . $zipcode . "'");
 
header("Content-type: text/xml");
 
$xml_output  = "<?xml version=\"1.0\"?>\n";
 
while($row = mysql_fetch_array($result)){
    $xml_output .= "\t<citystate>\n";
    $xml_output .= "\t\t<city>" . $row['city'] . "</city>\n";
    $xml_output .= "\t\t<state>" . $row['state'] . "</state>\n";
    $xml_output .= "\t</citystate>\n";
}
 
echo $xml_output;
 
mysql_close($con);
?>

6 Responses to “Don’t ever…”

Darren wrote a comment on July 14, 2009

Do you know what’s worse? Those credit card petrol pumps that make you put your zip code in. Fine if your credit card belongs to a US address, not so fine if your post code doesn’t and worse has letters in it. Having said that, if you put 90210 in (the only valid zip code I know), it still works 50% of the time.

David Singer wrote a comment on July 15, 2009

Actually, 5-digit-ZIP-to-city is NOT a unique mapping. 95030 maps to both Los Gatos and Monte Sereno (in California).

Nine-digit ZIPs do uniquely map to cities, but most people don’t know their 9-digit ZIP.

fjania wrote a comment on July 16, 2009

Interesting point David. According to usps.gov, 95030 maps to Los Gatos as the “actual name”, and they list Monte Sereno as an “acceptable alternative”. So I wonder if there are any any colliding addresses in 95030. I would assume not and that city name wouldn’t further disambiguate.

Diego wrote a comment on September 1, 2009

Actually, even the 9 digit info is not precise (at least in Brazil). Sometimes it points to an “aggregation” point (a city in which all mail gets sorted before reaching its final destination), even if the number is unique for a specific city. Those aggregations are used for optimizing delivery.

I would recomend though that the page automatically _suggest_ your location based on your postal code.

jtam wrote a comment on November 30, 2009

same with credit cards. there is no reason to choose mastercard or visa since their numbers map uniquely.

@darren: 90210 – ROTFLMAO

David Singer wrote a comment on March 7, 2010

I’m pretty sure that street addresses don’t collide between Monte Sereno and Los Gatos — since the two are served by the same police department, I’d think any collisions would have been sorted out in the interest of public safety.

Care to comment?