Forums

ASP

This topic is locked

Enter a zip and auto populate City and State

Posted 27 Feb 2009 17:19:53
1
has voted
27 Feb 2009 17:19:53 Michelle Weiler posted:
I have a form set up in asp using Dreamweaver with a text box for Zip, City, and State. I want a person to fill in zip and then when they move to the next field to have it fill in city and state. I also have a database called zipcodes and a table called zip with zip, city, state fields. Can anyone tell me how to do this. I have tried so many different things and just can't get it to work. []

Replies

Replied 25 Mar 2009 05:45:37
25 Mar 2009 05:45:37 dave blohm replied:
hi michelle...

so i tinkered around with this for a little bit and here's the solution i came up with...

first, let me apologize...i wrote this quickly so it's a little ugly.

there are two pages...the display page (index.asp) and the page serving up the data for the AJAX call (zips_ajax.asp). I have a table in my DB called ZIPS with fields 'zip', 'city', and 'state'.

here's a link to the functioning page on my site: www.daveblohm.com/dmxzone/zips/
here's a link to the CSV file containing the ZIPs for most if not all US cities (found this online for free after a little searching) : www.daveblohm.com/dmxzone/zips/allzips.csv

here's the code for index.asp

for some reason my_count is coming out as my_countmy_count in a couple places (lines 11 and 14) when I copy and paste my code...it should just be my_count

<
%' ::::: my func to build a states select field
function frm_select_states(field_name, show_selected, selected_value)
	response.write "<select name=""" & field_name & """ id=""" & field_name & """><option value="""">select a state</option>" & vbcrlf
	my_states = array("AL","Alabama","AK","Alaska","AZ","Arizona","AR","Arkansas","CA","California","CO","Colorado","CT","Connecticut","DE","Delaware","DC","District of Columbia","FL","Florida","GA","Georgia","HI","Hawaii","ID","Idaho","IL","Illinois","IN","Indiana","IA","Iowa","KS","Kansas","KY","Kentucky","LA","Louisiana","ME","Maine","MD","Maryland","MA","Massachusetts","MI","Michigan","MN","Minnesota","MS","Mississippi","MO","Missouri","MT","Montana","NE","Nebraska","NV","Nevada","NH","New Hampshire","NJ","New Jersey","NM","New Mexico","NY","New York","NC","North Carolina","ND","North Dakota","OH","Ohio","OK","Oklahoma","OR","Oregon","PA","Pennsylvania","PR","Puerto Rico","RI","Rhode Island","SC","South Carolina","SD","South Dakota","TN","Tennessee","TX","Texas","UT","Utah","VT","Vermont","VA","Virginia","WA","Washington","WV","West Virginia","WI","Wisconsin","WY","Wyoming")
	my_ubound = ubound(my_states)
	do while not my_count = my_ubound + 1
		response.write "<option value=""" & my_states(my_count) & """ "
		if show_selected = true and lcase(selected_value) =lcase(my_states(my_count)) then
			response.write " selected=""selected"" "
		end if 
		my_count = my_count + 1
		response.Write  ">" & my_states(my_count) & "</option>" & vbcrlf
		my_count = my_count + 1
	loop
	response.write "</select>"
end function
%>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DMX Zone Demo : ZIPS</title>
<script language="javascript">
function get_zip_info(zip){
	// pass the zip to zips_ajax.asp, return the city and state 
	// and set the form field values accrodingly
	var xmlHttp;
	try{
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch(e){
  		// Internet Explorer
  		try{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch(e){
    		try{
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		}
    		catch(e){
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}
  	xmlHttp.onreadystatechange=function(){
    	if(xmlHttp.readyState==4){
			// grab the returned string, split it, and populate the form fields
			var myarray = xmlHttp.responseText.split("*");
			if(myarray[0]=='eof'){
				alert('no data was returned');
				document.getElementById('city').value = '';
				document.getElementById('state').value = '';
			}else{
				document.getElementById('city').value = myarray[0];
				document.getElementById('state').value = myarray[1];				
			}
				document.getElementById('zip').focus();
      	}
    }
  	xmlHttp.open('GET','zips_ajax.asp?zip=' + zip,true);
  	xmlHttp.send(null);
}
</script>
</head>
<body onload="document.getElementById('zip').focus()">
	<table width="400" border="0" cellspacing="0" cellpadding="0" align="center">
		<tr>
			<td width="101"> </td>
			<td width="299"> </td>
		</tr>
		<tr>
			<td>City</td>
			<td><input type="text" name="city" id="city" /></td>
		</tr>
		<tr>
			<td>State</td>
			<td><% frm_select_states "state", false, false %></td>
		</tr>
		<tr>
			<td>ZIP Code</td>
			<td><input name="zip" type="text" id="zip" onchange="get_zip_info(this.value)" size="5" maxlength="5" /></td>
		</tr>
	</table>
</body>
</html>


here's the code for zips_ajax.asp

<!--#include virtual="/your_connections_file.asp" -->
<

%Set rs_cmd = server.createobject ("ADODB.Command")
rs_cmd.activeConnection = MM_conny_STRING
rs_cmd.commandtext = "select * from dbo.zips  where zip ='" & request.QueryString("zip") & "'"
rs_cmd.prepared = true
Set rs = rs_cmd.execute

if rs.eof then
	response.write "eof*"
else
	response.write  rs("city") & "*" & rs("state")
end if

rs.Close()
Set rs = Nothing
%>


let me know if you have any questions...hope this helps


Edited by - dave blohm on 25 Mar 2009  05:51:05

Reply to this topic