Don’t ever…

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)


while($row = mysql_fetch_array($result)){
    $xml_output .= "\t\n";
    $xml_output .= "\t\t" . $row['city'] . "\n";
    $xml_output .= "\t\t" . $row['state'] . "\n";
    $xml_output .= "\t\n";

echo $xml_output;



  1. Darren

    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.

  2. David Singer

    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.

  3. fjania

    Interesting point David. According to, 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.

  4. Diego

    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.

  5. jtam

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

    @darren: 90210 – ROTFLMAO

  6. David Singer

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *