Results 1 to 4 of 4

Thread: How to implement a zipcode lookup field

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default How to implement a zipcode lookup field

    I have a table of 43k zipcodes and I want to let users who are entering client addresses into a form enter the zipcode and have the city & state automatically fill in.

    I tried this code but nothing shows up in the City State field...

    Code:
    <?php include_once('db-connectdb.php'); $sql = "SELECT citystate FROM zipcode WHERE zip = '".$_POST['zip']."'";	
    $result = mysql_query($sql,$connection) or die("Couldn't execute $sql query. <br> mysql error: ".mysql_error());
    $row = mysql_fetch_row($result); $citystate = $row['citystate']; ?> 
    <p><label>City, State:</label><input type="text" name="citystate" size="40" value="<?php echo $citystate;?>"> Zip: <input type="text" name="zip" size="15"></p>
    If they have already typed in a city, I'm not sure if it should replace what they typed either. Can anyone see what I am doing wrong? Thanks!

  2. #2
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Smile

    Please see if writing like this as below works.
    <?php
    include_once('db-connectdb.php');
    $sql = "SELECT citystate FROM zipcode WHERE zip = '".$_POST['zip']."'";
    $result = mysql_query($sql,$connection) or die("Couldn't execute $sql query. <br> mysql error: ".mysql_error());
    $row = mysql_fetch_array($result,MYSQL_ASSOC);
    $citystate = $row['citystate'];
    ?>
    <p>
    <label>City, State:</label>
    <input type="text" name="citystate" size="40" value="<?php echo $citystate;?>">
    Zip: <input type="text" name="zip" size="15">
    </p>

  3. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Katierosy: I tried that code but nothing showed up in the citystate field when I entered the zip code. Thanks for trying. I'm surprised this isn't a very common script available everywhere, but no one seems to know how to make it work. I'd be happy to share the table of 43,377 zip codes if you can help me get it to work.

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Are you submitting the form when you enter the zip, if not this will need to use javascript in some sort.
    Corrections to my coding/thoughts welcome.

  5. The Following User Says Thank You to bluewalrus For This Useful Post:

    kuau (06-09-2010)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •