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
- /*======================================================================
- ** ZIP Code Lookup for USA and Canada
- **
- ** This PHP Script requires 2 GET parameters: zipcode, country (us/ca)
- ** Plus the database tables us and ca containing the ZIP Code-Lon/Lat.
- **
- ** Example call: tools_lookup.php?country=us&zipcode=90210
- **
- ** © 2012 https://www.zipcodesoft.com, All Rights Reserved
- **======================================================================
- */
- /* ----------------------------- */
- /* Connecting to MySQL server: */
- /* ----------------------------- */
- @mysql_connect($CFG_DB['db_host'], $CFG_DB['db_user'], $CFG_DB['db_pass'])
- or die("Error: mysql_connect() failed");
- /* ----------------------------- */
- /* Selecting client character set: */
- /* ----------------------------- */
- mysql_set_charset('utf8');
- /* ----------------------------- */
- /* Selecting database: */
- /* ----------------------------- */
- @mysql_select_db($CFG_DB['db_base'])
- or die("Error: mysql_select_db() failed");
- /* ----------------------------- */
- /* Get info for a given ZIP Code value */
- /* ----------------------------- */
- function getInfoByZip($sCountry, $sZipValue) {
- if (!($sZipName= getZipName($sCountry))) return false;
- $sql= "SELECT * FROM `$sCountry` WHERE `$sZipName`='$sZipValue' LIMIT 1";
- if (!($h_res= mysql_query($sql)) || !mysql_num_rows($h_res)) return false;
- $b_ok= ($a_row= mysql_fetch_assoc($h_res)) && count($a_row);
- mysql_free_result($h_res);
- return $b_ok? $a_row : false;
- }
- /* ----------------------------- */
- /* Start of Script */
- /* Get parameters */
- /* ----------------------------- */
- $b_ok= isset($_REQUEST['zipcode']) && isset($_REQUEST['country']);
- if (!$b_ok)
- die("Error: parameters are missed");
- $sZipCode = $_REQUEST['zipcode'];
- $sCountry = $_REQUEST['country'];
- if (!($a_info = getInfoByZip($sCountry, $sZipCode)))
- die("Error: zipcode not found");
- $sCity = $a_info["city"];
- $sLongitude = $a_info["longitude"];
- $sLatitude = $a_info["latitude"];
- if ($sCountry == "us") {
- $sZipTitle = "ZIP Code". ": ". $sZipCode;
- $sArea = "State: ". $a_info["state"]. " (". $a_info["statecode"]. ")";
- }
- else {
- $sZipTitle = "Postal Code". ": ". $sZipCode;
- $sArea = "Province: ". $a_info["province"]. " (". $a_info["provincecode"]. ")";
- }
- ?><!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
- <meta charset="utf-8">
- <title>ZIP Code Lookup</title>
- <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
- <script src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/src/infobox.js"></script>
- <script>
- function init() {
- // Creates a Google Map with the ZIP Code location as centerpoint.
- var mapCenter = new google.maps.LatLng(<?=$sLatitude?>,<?=$sLongitude?>);
- var mapOptions = {
- zoom: 12,
- center: mapCenter,
- mapTypeId: google.maps.MapTypeId.ROADMAP,
- disableDefaultUI: false
- }
- var map = new google.maps.Map(document.getElementById("map"), mapOptions);
- var marker1 = new google.maps.Marker({
- position: mapCenter,
- map: map,
- });
- }
- </script>
- </head>
- <body onload="init()" >
- <div id="result">
- <h1><?=$sZipTitle?></h1>
- City: <?=$sCity?>
- <?=$sArea?>
- Longitude: <?=$sLongitude?>
- Latitude: <?=$sLatitude?>
- </div>
- <div id="map" style="width: 252px; height: 252px;"></div>
- </body>
- </html>
ASP Script - Source Code
- <%
- '======================================================================
- ' ZIP Code Lookup for USA and Canada
- '
- ' This ASP Script requires 2 GET parameters: zipcode and country (us/ca)
- ' Plus the database tables us and ca containing the ZIP Code-Lon/Lat data provided by ZIPCodeSoft.
- '
- ' Example call: tools_lookup_asp.asp?country=us&zipcode=90210
- '
- ' © 2012 http://www.zipcodesoft.com, All Rights Reserved
- '======================================================================
- Dim strZIPCode
- Dim strCountry
- Dim strZIPName
- Dim strCity
- Dim strLongitude
- Dim strLatitude
- Dim strArea
- Dim strAreaCode
- '======================================================================
- ' Read the two required parameters
- '======================================================================
- strZIPCode = Request("zipcode")
- strCountry = Request("country")
- '======================================================================
- ' Set the correct term: ZIP Code = USA or Postal Code = Canada
- '======================================================================
- If strCountry="us" Then strZIPName = "ZIP Code"
- If strCountry="ca" Then strZIPName = "Postal Code"
- '======================================================================
- ' Open database connection and retrieve data.
- ' Please adjust to the database settings on your server.
- '======================================================================
- Set conn = Server.CreateObject("ADODB.Connection")
- conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.Mappath("/db/geo.mdb")
- Set rs = Server.CreateObject("ADODB.Recordset")
- If strCountry="us" Then sql = "SELECT * FROM us WHERE zipcode='" & strZIPCode & "';"
- If strCountry="ca" Then sql = "SELECT * FROM ca WHERE postalcode='" & strZIPCode & "';"
- Set rs = conn.Execute(sql)
- If Not rs.EOF Then
- strCity = rs.Fields("city")
- strLongitude = rs.Fields("longitude")
- strLatitude = rs.Fields("latitude")
- If strCountry="us" Then
- strArea = rs.Fields("state")
- strAreaCode = rs.Fields("statecode")
- End If
- If strCountry="ca" Then
- strArea = rs.Fields("province")
- strAreaCode = rs.Fields("provincecode")
- End If
- Else
- Response.Write(strZIPName & " Not found")
- End If
- rs.Close
- Set rs = Nothing
- Set conn = Nothing
- Function iif(condition,isTrue,isFalse)
- If (condition) Then
- iif = isTrue
- Else
- iif = isFalse
- End If
- End Function
- %>
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
- <meta charset="utf-8">
- <title>ZIP Code Lookup</title>
- <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
- <script src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/src/infobox.js"></script>
- <script>
- function init() {
- // Creates a Google Map with the ZIP Code location as centerpoint.
- var mapCenter = new google.maps.LatLng(<%=strLatitude%>,<%=strLongitude%>);
- var mapOptions = {
- zoom: 12,
- center: mapCenter,
- mapTypeId: google.maps.MapTypeId.ROADMAP,
- disableDefaultUI: false
- }
- var map = new google.maps.Map(document.getElementById("map"), mapOptions);
- var marker1 = new google.maps.Marker({
- position: mapCenter,
- map: map,
- });
- }
- </script>
- </head>
- <body onload="init()" >
- <div id="result">
- <h1><%=strZIPName & ": " & strZIPCode%></h1>
- City: <%=strCity%>
- <%=iif(strCountry="ca","Province: ","State: ") & strArea & " (" & strAreaCode & ")"%>
- Longitude: <%=strLongitude%>
- Latitude: <%=strLatitude%>
- </div>
- <div id="map" style="width: 252px; height: 252px;"></div>
- </body>
- </html>
C# Script (ASP.NET)- Source Code 
- Tools_Lookup.aspx:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Tools_Lookup.aspx.cs" Inherits="ZIPCodeTools.Tools_Lookup" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Distance between 2 ZIP Codes</title>
- <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
- <script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/src/infobox.js"></script>
- <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
- <%--<script type="text/javascript">
- $(document).ready(function () {
- initialize();
- });
- </script>--%>
- <asp:Literal ID="litHeaderCode" runat="server"></asp:Literal>
- </head>
- <body onload="$(document).ready(function () { initialize(); });">
- <form id="form1" runat="server">
- <asp:Panel ID="result" runat="server">
- <h1><asp:Label ID="lbl_zip" runat="server" /> </h1>
- City: <asp:Label ID="lbl_city" runat="server" />
- <asp:Label ID="lbl_area" runat="server" />
- Longitude: <asp:Label ID="lbl_longitude" runat="server" />
- Latitude: <asp:Label ID="lbl_latitude" runat="server" />
- </asp:Panel>
- <div>
- <asp:literal ID="litMessages" runat="server"></asp:literal>
- <div id="map" style="width: 300px; height: 300px;"></div>
- <div id="outputDiv" style="width: 300px; height: 50px;" ></div>
- </div>
- </form>
- </body>
- </html>
- Tools_Lookup.aspx.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data.SqlClient;
- using System.Data;
- using System.Text;
- namespace ZIPCodeTools
- {
- public partial class Tools_Lookup : System.Web.UI.Page
- {
- DataTools dt = new DataTools();
- protected void Page_Load(object sender, EventArgs e)
- {
- string ErrorMessage = "";
- if (Request.Params["zipcode"] == null || Request.Params["zipcode"].Length == 0)
- {
- ErrorMessage += "<li>Parameter zipcode1 cannot be missing or blank</li>";
- }
- if (Request.Params["country"] == null || Request.Params["country"].Length == 0)
- {
- ErrorMessage += "<li>Parameter country1 cannot be missing or blank</li>";
- }
- if (ErrorMessage.Length > 0)
- {
- litMessages.Text = String.Format("<ul>{0}</ul>", ErrorMessage);
- }
- else
- Calculate();
- }
- public void Calculate()
- {
- try
- {
- string country = Request["country"].ToString().ToLower();
- SqlCommand cmd = new SqlCommand();
- if (country.Equals("us"))
- cmd.CommandText = "SELECT * FROM us WHERE zipcode = @Zipcode;";
- else if ( country.Equals("ca") )
- cmd.CommandText = "SELECT * FROM ca WHERE postalcode = @Zipcode;";
- cmd.Parameters.Add("@ZipCode", SqlDbType.VarChar, 5).Value = Request["zipcode"].ToString();
- DataSet ds = dt.GetDataSet(cmd, DataTools.DataSources.zipcodesoft);
- string strCity = string.Empty;
- string strLongitude = string.Empty;
- string strLatitude = string.Empty;
- string strArea = string.Empty;
- string strAreaCode = string.Empty;
- string strZIPName = string.Empty;
- string strZIPCode = Request["zipcode"].ToString();
- if (country.Equals("us"))
- strZIPName = "ZIP Code";
- else if ( country.Equals("ca") )
- strZIPName = "Postal Code";
- if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
- {
- DataRow dr = ds.Tables[0].Rows[0];
- strCity = dr["city"].ToString();
- strLongitude = dr["longitude"].ToString();
- strLatitude = dr["latitude"].ToString();
- if (country.Equals("us"))
- {
- strArea = dr["state"].ToString();
- strAreaCode = dr["statecode"].ToString();
- }
- else if (country.Equals("ca"))
- {
- strArea = dr["province"].ToString();
- strAreaCode = dr["provincecode"].ToString();
- }
- }
- lbl_zip.Text = strZIPName + ": " + strZIPCode;
- lbl_city.Text = strCity;
- lbl_longitude.Text = strLongitude;
- lbl_latitude.Text = strLatitude;
- if (country.Equals("ca"))
- lbl_area.Text = "Province: " + strArea + " (" + strAreaCode + ")";
- else if ( country.Equals("us") )
- lbl_area.Text = "State: " + strArea + " (" + strAreaCode + ")";
- //Begin Creating Website Javascript Code
- StringBuilder output = new StringBuilder();
- output.Append(Environment.NewLine);
- output.Append("// Creates a Google Map with the ZIP Code location as centerpoint.");
- output.Append(Environment.NewLine);
- output.Append("function initialize() {");
- output.Append(Environment.NewLine);
- output.Append("var mapCenter = new google.maps.LatLng(").Append(strLatitude).Append(",").Append(strLongitude).Append(");");
- output.Append(Environment.NewLine);
- output.Append("var mapOptions = {");
- output.Append(Environment.NewLine);
- output.Append("zoom: 12,");
- output.Append(Environment.NewLine);
- output.Append("center: mapCenter,");
- output.Append(Environment.NewLine);
- output.Append("mapTypeId: google.maps.MapTypeId.ROADMAP, disableDefaultUI: false");
- output.Append(Environment.NewLine);
- output.Append("};");
- output.Append(Environment.NewLine);
- output.Append("var map = new google.maps.Map(document.getElementById(\"map\"), mapOptions);");
- output.Append(Environment.NewLine);
- output.Append("var marker1 = new google.maps.Marker({ position: mapCenter, map: map, });");
- output.Append(Environment.NewLine);
- output.Append("}");
- Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptAPI", output.ToString(), true);
- }
- catch (Exception ex)
- {
- }
- }
- }
- }
Coldfusion Script - Source Code 
- <!--- '======================================================================
- ' ZIP Code Lookup for USA and Canada
- '
- ' This CFM Script requires 2 GET parameters: zipcode and country (us/ca)
- ' Plus the database tables us and ca containing the ZIP Code-Lon/Lat.
- '
- ' Example call: tools_lookup_cfm.cfm?country=us&zipcode=90210
- '
- ' © 2012 www.zipcodesoft.com, All Rights Reserved
- '====================================================================== --->
- <cfsetting showdebugoutput="false">
- <!--- This is data source name defined in coldfusion administrator --->
- <cfset rs = "geodb">
- <cfset strZIPCode = "">
- <cfset strCountry = "">
- <cfset strZIPName = "">
- <cfset strCity = "">
- <cfset strLongitude = "">
- <cfset strLatitude = "">
- <cfset strArea = "">
- <cfset strAreaCode = "">
- <!--- '======================================================================
- ' Read the two required parameters
- '====================================================================== --->
- <cfset strZIPCode = URL.zipcode>
- <cfset strCountry = URL.country>
- <!--- '======================================================================
- ' Set the correct term: ZIP Code = USA or Postal Code = Canada
- '====================================================================== --->
- <cfif strCountry eq "us">
- <cfset strZIPName = "ZIP Code">
- <cfelseif strCountry eq "ca">
- <cfset strZIPName = "Postal Code">
- </cfif>
- <cfif strCountry eq "us">
- <cfquery name="getZipcodes" datasource="#rs#">
- SELECT * FROM us WHERE zipcode='#strZIPCode#'
- </cfquery>
- <cfelseif strCountry eq "ca">
- <cfquery name="getZipcodes" datasource="#rs#">
- SELECT * FROM ca WHERE postalcode='#strZIPCode#'
- </cfquery>
- </cfif>
- <cfif getZipcodes.recordcount gt 0>
- <cfset strCity = getZipcodes.city>
- <cfset strLongitude = getZipcodes.longitude>
- <cfset strLatitude = getZipcodes.latitude>
- <cfif strCountry eq "us">
- <cfset strArea = getZipcodes.state>
- <cfset strAreaCode = getZipcodes.statecode>
- </cfif>
- <cfif strCountry eq "ca">
- <cfset strArea = getZipcodes.province>
- <cfset strAreaCode = getZipcodes.provincecode>
- </cfif>
- <cfelse>
- <cfoutput>#strZIPName# Not found</cfoutput>
- </cfif>
- <cfoutput>
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
- <meta charset="utf-8">
- <title>ZIP Code Lookup</title>
- <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
- <script src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/src/infobox.js"></script>
- <script>
- function init() {
- // Creates a Google Map with the ZIP Code location as centerpoint.
- var mapCenter = new google.maps.LatLng(#strLatitude#,#strLongitude#);
- var mapOptions = {
- zoom: 12,
- center: mapCenter,
- mapTypeId: google.maps.MapTypeId.ROADMAP,
- disableDefaultUI: false
- }
- var map = new google.maps.Map(document.getElementById("map"), mapOptions);
- var marker1 = new google.maps.Marker({
- position: mapCenter,
- map: map,
- });
- }
- </script>
- </head>
- <body onload="init()" >
- <div id="result">
- <h1>#strZIPName & ": " & strZIPCode#</h1>
- City: #strCity#
- <cfif strCountry eq "ca">
- Province: #strArea# (#strAreaCode#)
- <cfelse>
- State: #strArea# (#strAreaCode#)
- </cfif>
- Longitude: #strLongitude#
- Latitude: #strLatitude#
- </div>
- <div id="map" style="width: 252px; height: 252px;"></div>
- </body>
- </html>
- </cfoutput>