ZIP Code Lookup

Test our free ZIP Code Lookup and download our free scripts that are perfectly compatible with our ZIP Code databases.





Usage


Present a ZIP Code Lookup feature on your website to enhance your websites functionality.

PHP Script - Source Code PHP Script


  1. <?php  
  2. /*====================================================================== 
  3. ** ZIP Code Lookup for USA and Canada 
  4. ** 
  5. ** This PHP Script requires 2 GET parameters: zipcode, country (us/ca) 
  6. ** Plus the database tables us and ca containing the ZIP Code-Lon/Lat. 
  7. ** 
  8. ** Example call: tools_lookup.php?country=us&zipcode=90210 
  9. **  
  10. ** © 2012 https://www.zipcodesoft.com, All Rights Reserved 
  11. **====================================================================== 
  12. */  
  13.   
  14. /* ----------------------------- */  
  15. /* Connecting to MySQL server: */  
  16. /* ----------------------------- */  
  17. @mysql_connect($CFG_DB['db_host'], $CFG_DB['db_user'], $CFG_DB['db_pass'])  
  18.     or die("Error: mysql_connect() failed");  
  19.   
  20. /* ----------------------------- */  
  21. /* Selecting client character set: */  
  22. /* ----------------------------- */  
  23. mysql_set_charset('utf8');  
  24.   
  25. /* ----------------------------- */  
  26. /* Selecting database: */  
  27. /* ----------------------------- */  
  28. @mysql_select_db($CFG_DB['db_base'])  
  29.     or die("Error: mysql_select_db() failed");  
  30.   
  31. /* ----------------------------- */  
  32. /* Get info for a given ZIP Code value */  
  33. /* ----------------------------- */  
  34. function getInfoByZip($sCountry$sZipValue) {  
  35.     if (!($sZipName= getZipName($sCountry))) return false;  
  36.     $sql"SELECT * FROM `$sCountry` WHERE `$sZipName`='$sZipValue' LIMIT 1";  
  37.     if (!($h_res= mysql_query($sql)) || !mysql_num_rows($h_res)) return false;  
  38.     $b_ok= ($a_row= mysql_fetch_assoc($h_res)) && count($a_row);  
  39.     mysql_free_result($h_res);  
  40.     return $b_ok$a_row : false;  
  41. }  
  42.   
  43. /* ----------------------------- */  
  44. /* Start of Script */  
  45. /* Get parameters */  
  46. /* ----------------------------- */  
  47. $b_ok= isset($_REQUEST['zipcode']) && isset($_REQUEST['country']);  
  48. if (!$b_ok)  
  49.     die("Error: parameters are missed");  
  50.   
  51. $sZipCode = $_REQUEST['zipcode'];  
  52. $sCountry = $_REQUEST['country'];  
  53.   
  54. if (!($a_info = getInfoByZip($sCountry$sZipCode)))  
  55.     die("Error: zipcode not found");  
  56.   
  57. $sCity = $a_info["city"];  
  58. $sLongitude = $a_info["longitude"];  
  59. $sLatitude = $a_info["latitude"];  
  60. if ($sCountry == "us") {  
  61.     $sZipTitle = "ZIP Code"": "$sZipCode;  
  62.     $sArea = "State: "$a_info["state"]. " ("$a_info["statecode"]. ")";  
  63. }  
  64. else {  
  65.     $sZipTitle = "Postal Code"": "$sZipCode;  
  66.     $sArea = "Province: "$a_info["province"]. " ("$a_info["provincecode"]. ")";  
  67. }  
  68.   
  69.   
  70. ?><!DOCTYPE html>  
  71. <html>  
  72.     <head>  
  73.         <meta name="viewport" content="initial-scale=1.0, user-scalable=no">  
  74.         <meta charset="utf-8">  
  75.         <title>ZIP Code Lookup</title>  
  76.   
  77.         <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>  
  78.         <script src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/src/infobox.js"></script>  
  79.         <script>  
  80.   
  81.         function init() {  
  82.             // Creates a Google Map with the ZIP Code location as centerpoint.  
  83.             var mapCenter = new google.maps.LatLng(<?=$sLatitude?>,<?=$sLongitude?>);  
  84.             var mapOptions = {  
  85.                 zoom: 12,  
  86.                 center: mapCenter,  
  87.                 mapTypeId: google.maps.MapTypeId.ROADMAP,  
  88.                 disableDefaultUI: false  
  89.             }  
  90.             var map = new google.maps.Map(document.getElementById("map"), mapOptions);  
  91.             var marker1 = new google.maps.Marker({  
  92.                 position: mapCenter,  
  93.                 map: map,  
  94.             });  
  95.         }  
  96.         </script>  
  97.     </head>  
  98.     <body onload="init()" >  
  99.     <div id="result">         
  100.         <h1><?=$sZipTitle?></h1>  
  101.         City: <?=$sCity?>  
  102.   
  103.         <?=$sArea?>  
  104.   
  105.   
  106.         Longitude: <?=$sLongitude?>  
  107.   
  108.         Latitude: <?=$sLatitude?>  
  109.     </div>  
  110.     <div id="map" style="width: 252px; height: 252px;"></div>  
  111.     </body>  
  112. </html>  
Download PHP script with sample database

ASP Script - Source CodeASP Script


  1. <%  
  2. '======================================================================  
  3. ' ZIP Code Lookup for USA and Canada  
  4. '  
  5. ' This ASP Script requires 2 GET parameters: zipcode and country (us/ca)  
  6. ' Plus the database tables us and ca containing the ZIP Code-Lon/Lat data provided by ZIPCodeSoft.  
  7. '  
  8. ' Example call: tools_lookup_asp.asp?country=us&zipcode=90210  
  9. '   
  10. ' © 2012 http://www.zipcodesoft.com, All Rights Reserved  
  11. '======================================================================  
  12.   
  13. Dim strZIPCode  
  14. Dim strCountry  
  15. Dim strZIPName  
  16. Dim strCity  
  17. Dim strLongitude  
  18. Dim strLatitude  
  19. Dim strArea  
  20. Dim strAreaCode  
  21.   
  22. '======================================================================  
  23. ' Read the two required parameters  
  24. '======================================================================  
  25. strZIPCode = Request("zipcode")  
  26. strCountry = Request("country")  
  27.   
  28. '======================================================================  
  29. ' Set the correct term: ZIP Code = USA or Postal Code = Canada  
  30. '======================================================================  
  31. If strCountry="us" Then strZIPName = "ZIP Code"  
  32. If strCountry="ca" Then strZIPName = "Postal Code"  
  33.   
  34. '======================================================================  
  35. ' Open database connection and retrieve data.   
  36. ' Please adjust to the database settings on your server.  
  37. '======================================================================  
  38. Set conn =  Server.CreateObject("ADODB.Connection")  
  39. conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.Mappath("/db/geo.mdb")  
  40. Set rs = Server.CreateObject("ADODB.Recordset")  
  41.   
  42. If strCountry="us" Then sql = "SELECT * FROM us WHERE zipcode='" & strZIPCode & "';"  
  43. If strCountry="ca" Then sql = "SELECT * FROM ca WHERE postalcode='" & strZIPCode & "';"  
  44.   
  45. Set rs = conn.Execute(sql)  
  46.   
  47. If Not rs.EOF Then  
  48.     strCity = rs.Fields("city")  
  49.     strLongitude = rs.Fields("longitude")  
  50.     strLatitude = rs.Fields("latitude")  
  51.     If strCountry="us" Then  
  52.         strArea = rs.Fields("state")  
  53.         strAreaCode = rs.Fields("statecode")  
  54.     End If  
  55.     If strCountry="ca" Then  
  56.         strArea = rs.Fields("province")  
  57.         strAreaCode = rs.Fields("provincecode")  
  58.     End If  
  59. Else  
  60.     Response.Write(strZIPName & " Not found")  
  61. End If  
  62.   
  63. rs.Close  
  64. Set rs = Nothing  
  65. Set conn = Nothing     
  66.   
  67. Function iif(condition,isTrue,isFalse)  
  68.     If (condition) Then  
  69.        iif = isTrue  
  70.     Else  
  71.        iif = isFalse  
  72.     End If  
  73. End Function  
  74. %>  
  75. <!DOCTYPE html>  
  76. <html>  
  77.     <head>  
  78.         <meta name="viewport" content="initial-scale=1.0, user-scalable=no">  
  79.         <meta charset="utf-8">  
  80.         <title>ZIP Code Lookup</title>  
  81.   
  82.         <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>  
  83.         <script src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/src/infobox.js"></script>  
  84.         <script>  
  85.   
  86.         function init() {  
  87.             // Creates a Google Map with the ZIP Code location as centerpoint.  
  88.             var mapCenter = new google.maps.LatLng(<%=strLatitude%>,<%=strLongitude%>);  
  89.             var mapOptions = {  
  90.                 zoom: 12,  
  91.                 center: mapCenter,  
  92.                 mapTypeId: google.maps.MapTypeId.ROADMAP,  
  93.                 disableDefaultUI: false  
  94.             }  
  95.             var map = new google.maps.Map(document.getElementById("map"), mapOptions);  
  96.             var marker1 = new google.maps.Marker({  
  97.                 position: mapCenter,  
  98.                 map: map,  
  99.             });  
  100.         }  
  101.         </script>  
  102.     </head>  
  103.     <body onload="init()" >  
  104.     <div id="result">         
  105.         <h1><%=strZIPName & ": " & strZIPCode%></h1>  
  106.         City: <%=strCity%>  
  107.   
  108.         <%=iif(strCountry="ca","Province: ","State: ") & strArea & " (" & strAreaCode & ")"%>  
  109.   
  110.   
  111.         Longitude: <%=strLongitude%>  
  112.   
  113.         Latitude: <%=strLatitude%>  
  114.     </div>  
  115.     <div id="map" style="width: 252px; height: 252px;"></div>  
  116.     </body>  
  117. </html>  
Download ASP script with sample database

C# Script (ASP.NET)- Source Code C# Script


  1. Tools_Lookup.aspx:  
  2.   
  3. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Tools_Lookup.aspx.cs" Inherits="ZIPCodeTools.Tools_Lookup" %>  
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9.     <title>Distance between 2 ZIP Codes</title>  
  10.   
  11. <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>  
  12. <script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/src/infobox.js"></script>  
  13. <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>  
  14. <%--<script type="text/javascript">  
  15.     $(document).ready(function () {  
  16.         initialize();  
  17.     });  
  18. </script>--%>  
  19.   
  20. <asp:Literal ID="litHeaderCode" runat="server"></asp:Literal>  
  21.   
  22. </head>  
  23. <body onload="$(document).ready(function () { initialize(); });">  
  24.     <form id="form1" runat="server">  
  25.     <asp:Panel ID="result" runat="server">  
  26.         <h1><asp:Label ID="lbl_zip" runat="server" /> </h1>  
  27.         City: <asp:Label ID="lbl_city" runat="server" />  
  28.   
  29.         <asp:Label ID="lbl_area" runat="server" />   
  30.   
  31.   
  32.         Longitude: <asp:Label ID="lbl_longitude" runat="server" />  
  33.   
  34.         Latitude: <asp:Label ID="lbl_latitude" runat="server" />  
  35.   
  36.     </asp:Panel>  
  37.     <div>  
  38.         <asp:literal ID="litMessages" runat="server"></asp:literal>  
  39.         <div id="map" style="width: 300px; height: 300px;"></div>  
  40.         <div id="outputDiv" style="width: 300px; height: 50px;" ></div>  
  41.     </div>  
  42.     </form>  
  43. </body>  
  44.   
  45. </html>  
  46.   
  47.   
  48.   
  49. Tools_Lookup.aspx.cs:  
  50.   
  51. using System;  
  52. using System.Collections.Generic;  
  53. using System.Linq;  
  54. using System.Web;  
  55. using System.Web.UI;  
  56. using System.Web.UI.WebControls;  
  57. using System.Data.SqlClient;  
  58. using System.Data;  
  59. using System.Text;  
  60.   
  61. namespace ZIPCodeTools  
  62. {  
  63.     public partial class Tools_Lookup : System.Web.UI.Page  
  64.     {  
  65.         DataTools dt = new DataTools();  
  66.   
  67.         protected void Page_Load(object sender, EventArgs e)  
  68.         {  
  69.             string ErrorMessage = "";  
  70.   
  71.             if (Request.Params["zipcode"] == null || Request.Params["zipcode"].Length == 0)  
  72.             {  
  73.                 ErrorMessage += "<li>Parameter zipcode1 cannot be missing or blank</li>";  
  74.             }  
  75.   
  76.             if (Request.Params["country"] == null || Request.Params["country"].Length == 0)  
  77.             {  
  78.                 ErrorMessage += "<li>Parameter country1 cannot be missing or blank</li>";  
  79.             }              
  80.   
  81.             if (ErrorMessage.Length > 0)  
  82.             {  
  83.                 litMessages.Text = String.Format("<ul>{0}</ul>", ErrorMessage);  
  84.             }  
  85.             else  
  86.                 Calculate();  
  87.         }  
  88.   
  89.         public void Calculate()  
  90.         {  
  91.             try  
  92.             {  
  93.                 string country = Request["country"].ToString().ToLower();  
  94.   
  95.                 SqlCommand cmd = new SqlCommand();  
  96.   
  97.                 if (country.Equals("us"))                  
  98.                     cmd.CommandText = "SELECT * FROM us WHERE zipcode = @Zipcode;";  
  99.                 else if ( country.Equals("ca") )  
  100.                     cmd.CommandText = "SELECT * FROM ca WHERE postalcode = @Zipcode;";  
  101.   
  102.                 cmd.Parameters.Add("@ZipCode", SqlDbType.VarChar, 5).Value = Request["zipcode"].ToString();  
  103.   
  104.                 DataSet ds = dt.GetDataSet(cmd, DataTools.DataSources.zipcodesoft);  
  105.                 string strCity = string.Empty;  
  106.                 string strLongitude = string.Empty;  
  107.                 string strLatitude = string.Empty;  
  108.                 string strArea = string.Empty;  
  109.                 string strAreaCode = string.Empty;  
  110.                 string strZIPName = string.Empty;  
  111.                 string strZIPCode = Request["zipcode"].ToString();  
  112.   
  113.                  if (country.Equals("us"))                  
  114.                     strZIPName = "ZIP Code";  
  115.                 else if ( country.Equals("ca") )  
  116.                     strZIPName = "Postal Code";  
  117.   
  118.                 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)  
  119.                 {  
  120.                     DataRow dr = ds.Tables[0].Rows[0];  
  121.                     strCity = dr["city"].ToString();  
  122.                     strLongitude = dr["longitude"].ToString();  
  123.                     strLatitude = dr["latitude"].ToString();  
  124.   
  125.                     if (country.Equals("us"))  
  126.                     {  
  127.                         strArea = dr["state"].ToString();  
  128.                         strAreaCode = dr["statecode"].ToString();  
  129.                     }  
  130.                     else if (country.Equals("ca"))  
  131.                     {  
  132.                         strArea = dr["province"].ToString();  
  133.                         strAreaCode = dr["provincecode"].ToString();  
  134.                     }  
  135.                 }  
  136.   
  137.                 lbl_zip.Text = strZIPName + ": " + strZIPCode;  
  138.                 lbl_city.Text = strCity;  
  139.                 lbl_longitude.Text = strLongitude;  
  140.                 lbl_latitude.Text = strLatitude;  
  141.                 if (country.Equals("ca"))  
  142.                     lbl_area.Text = "Province: " + strArea + " (" + strAreaCode + ")";  
  143.                 else if ( country.Equals("us") )  
  144.                     lbl_area.Text = "State: " + strArea + " (" + strAreaCode + ")";  
  145.   
  146.                 //Begin Creating Website Javascript Code  
  147.                 StringBuilder output = new StringBuilder();  
  148.   
  149.                 output.Append(Environment.NewLine);  
  150.                 output.Append("// Creates a Google Map with the ZIP Code location as centerpoint.");  
  151.                 output.Append(Environment.NewLine);  
  152.                 output.Append("function initialize() {");  
  153.                 output.Append(Environment.NewLine);  
  154.                 output.Append("var mapCenter = new google.maps.LatLng(").Append(strLatitude).Append(",").Append(strLongitude).Append(");");  
  155.                 output.Append(Environment.NewLine);  
  156.                 output.Append("var mapOptions = {");  
  157.                 output.Append(Environment.NewLine);  
  158.                 output.Append("zoom: 12,");  
  159.                 output.Append(Environment.NewLine);  
  160.                 output.Append("center: mapCenter,");  
  161.                 output.Append(Environment.NewLine);  
  162.                 output.Append("mapTypeId: google.maps.MapTypeId.ROADMAP, disableDefaultUI: false");  
  163.                 output.Append(Environment.NewLine);  
  164.                 output.Append("};");  
  165.                 output.Append(Environment.NewLine);  
  166.                 output.Append("var map = new google.maps.Map(document.getElementById(\"map\"), mapOptions);");  
  167.                 output.Append(Environment.NewLine);  
  168.                 output.Append("var marker1 = new google.maps.Marker({ position: mapCenter, map: map, });");  
  169.                 output.Append(Environment.NewLine);  
  170.                 output.Append("}");  
  171.   
  172.                 Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptAPI", output.ToString(), true);  
  173.             }  
  174.             catch (Exception ex)  
  175.             {  
  176.             }  
  177.         }  
  178.       
  179.     }  
  180. }  
Download C# script (ASP.NET) with sample database

Coldfusion Script - Source Code Coldfusion Script


  1. <!--- '======================================================================  
  2. ' ZIP Code Lookup for USA and Canada  
  3. '  
  4. ' This CFM Script requires 2 GET parameters: zipcode and country (us/ca)  
  5. ' Plus the database tables us and ca containing the ZIP Code-Lon/Lat.  
  6. '  
  7. ' Example call: tools_lookup_cfm.cfm?country=us&zipcode=90210  
  8. '   
  9. ' © 2012 www.zipcodesoft.com, All Rights Reserved  
  10. '====================================================================== --->  
  11.   
  12. <cfsetting showdebugoutput="false">  
  13.   
  14. <!--- This is data source name defined in coldfusion administrator --->  
  15. <cfset rs = "geodb">  
  16.   
  17. <cfset strZIPCode = "">  
  18. <cfset strCountry = "">  
  19. <cfset strZIPName = "">  
  20. <cfset strCity = "">  
  21. <cfset strLongitude = "">  
  22. <cfset strLatitude = "">  
  23. <cfset strArea = "">  
  24. <cfset strAreaCode = "">  
  25.   
  26. <!--- '======================================================================  
  27. ' Read the two required parameters  
  28. '====================================================================== --->  
  29.   
  30. <cfset strZIPCode = URL.zipcode>  
  31. <cfset strCountry = URL.country>  
  32.   
  33. <!--- '======================================================================  
  34. ' Set the correct term: ZIP Code = USA or Postal Code = Canada  
  35. '====================================================================== --->  
  36.   
  37. <cfif strCountry eq "us">  
  38.     <cfset strZIPName = "ZIP Code">  
  39. <cfelseif strCountry eq "ca">  
  40.     <cfset strZIPName = "Postal Code">  
  41. </cfif>  
  42.   
  43. <cfif strCountry eq "us">  
  44.     <cfquery name="getZipcodes"  datasource="#rs#">  
  45.         SELECT * FROM us WHERE zipcode='#strZIPCode#'  
  46.     </cfquery>  
  47. <cfelseif strCountry eq "ca">  
  48.     <cfquery name="getZipcodes"  datasource="#rs#">  
  49.         SELECT * FROM ca WHERE postalcode='#strZIPCode#'  
  50.     </cfquery>  
  51. </cfif>  
  52.   
  53.   
  54. <cfif getZipcodes.recordcount gt 0>  
  55.   
  56.     <cfset strCity = getZipcodes.city>  
  57.     <cfset strLongitude = getZipcodes.longitude>  
  58.     <cfset strLatitude = getZipcodes.latitude>  
  59.     <cfif strCountry eq "us">  
  60.         <cfset strArea = getZipcodes.state>  
  61.         <cfset strAreaCode = getZipcodes.statecode>  
  62.     </cfif>  
  63.     <cfif strCountry eq "ca">  
  64.         <cfset strArea = getZipcodes.province>  
  65.         <cfset strAreaCode = getZipcodes.provincecode>  
  66.     </cfif>  
  67. <cfelse>  
  68.     <cfoutput>#strZIPName# Not found</cfoutput>  
  69. </cfif>  
  70.   
  71.   
  72. <cfoutput>  
  73. <!DOCTYPE html>  
  74. <html>  
  75.     <head>  
  76.         <meta name="viewport" content="initial-scale=1.0, user-scalable=no">  
  77.         <meta charset="utf-8">  
  78.         <title>ZIP Code Lookup</title>  
  79.   
  80.         <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>  
  81.         <script src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/src/infobox.js"></script>  
  82.         <script>  
  83.   
  84.         function init() {  
  85.             // Creates a Google Map with the ZIP Code location as centerpoint.  
  86.             var mapCenter = new google.maps.LatLng(#strLatitude#,#strLongitude#);  
  87.             var mapOptions = {  
  88.                 zoom: 12,  
  89.                 center: mapCenter,  
  90.                 mapTypeId: google.maps.MapTypeId.ROADMAP,  
  91.                 disableDefaultUI: false  
  92.             }  
  93.             var map = new google.maps.Map(document.getElementById("map"), mapOptions);  
  94.             var marker1 = new google.maps.Marker({  
  95.                 position: mapCenter,  
  96.                 map: map,  
  97.             });  
  98.         }  
  99.         </script>  
  100.     </head>  
  101.     <body onload="init()" >  
  102.     <div id="result">         
  103.         <h1>#strZIPName & ": " & strZIPCode#</h1>  
  104.         City: #strCity#  
  105.   
  106.         <cfif strCountry eq "ca">  
  107.             Province: #strArea# (#strAreaCode#)  
  108.         <cfelse>  
  109.             State: #strArea# (#strAreaCode#)  
  110.         </cfif>  
  111.           
  112.   
  113.   
  114.         Longitude: #strLongitude#  
  115.   
  116.         Latitude: #strLatitude#  
  117.     </div>  
  118.     <div id="map" style="width: 252px; height: 252px;"></div>  
  119.     </body>  
  120. </html>  
  121. </cfoutput>  
Download Coldfusion script with sample database

Source Code available
for free download in


PHP Script

ASP Script Source Code

C# Source Code

CF Source Code

All these scripts work perfectly with our databases


ZIP Code Database USA

Postal Code Database Canada

ZIP Code Database USA / Canada