Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type Zip Code - auto fill city & state 1

Status
Not open for further replies.

schase

Technical User
Sep 7, 2001
1,756
US
Hi folks,

My javascript abilities are next to none. I've searched for this but have not been able to figure it out.

What I am looking to do is.

Type in a zip code in a text box. hit tab or click out of that text box - and if it matches in my database - than the city and State are Automatically filled, if not then it stays blank.

I use ASP and mySQL. I can easily pull the records out of the database - but am entirely unsure as to how to match and autofill.

The one other thing I would need to do is somehow flag if it is not previously matched. Like put $$ infront of the city if I type it in.

From that point I will write up the code to insert into the zip code table for future matches.

Any ideas on how to accomplish this?

thank you.

"Never underestimate the power of determination"

Stuart
Net+
 
thank you for the help here.

So now my script shows

Code:
<script type="text/javascript">
var nH = new ActiveXObject("Microsoft.XMLDOM")
nH.async="false"
nH.load("postcode.xml")
strSearch = "contains(@fldZip, '79924')"
var nH=tXML.selectNodes("/xml/rs:data/z:row["+ strSearch +"]");
for(var i=0;i<nH.length;i++){
alert(nH.item(i).attributes.getNamedItem("fldZip").value
}
</script>

What do i do with it? how do i get it into the textboxes where it will search depending on what is typed in - and fill the other two text boxes if there is a match?

"Never underestimate the power of determination"

Stuart
Net+
 
Ok, this may be too simple of a solution for the direction you guys are headed, but what do you think of using a hidden iframe to lookup just the one zip code?
Code:
<html>
<head>
<script language="JavaScript">
function fillCityState(formName,cityField,stateField,zip){
  var url="zipLookup.asp?formName="+escape(formName)+
    "&cityField="+escape(cityField)+
    "&stateField="+escape(stateField)+
    "&zip="+escape(zip);
  window.open(url,"ihidden");
}
</script>
</head><body>
<form name="formName">
<iframe name="ihidden" style="position:absolute;visibility:hidden"></iframe>
<input type="text" name="myCity">
<input type="text" name="myState">
<input type="text" name="Zip" onblur="fillCityState(this.form.name,'myCity','myState',this.value)">
</form>

The content of zipLookup.asp would look something like this:
Code:
<%
  [green]'Add your zip lookup ASP here...[/green]
%>
<script language="JavaScript">
try{
  var cityField = parent.document['<%=Request("formName")%>']['<%=Request("cityField")%>'];
  var stateField = parent.document['<%=Request("formName")%>']['<%=Request("stateField")%>'];
  cityField.value = '<%=Replace(city,"'","\'")%>';
  stateField.value = '<%=Replace(state,"'","\'")%>';
}catch(er){
  alert('Could not fill city and state automatically.');
}
</script>

Although I've seen instances of zip codes spanning multiple cities, so you may want this procedure to fill a drop-down menu instead.

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
i see what you are saying, you would need the IFrame to call the asp when its needed rather than all the time.

How would you carry those form values onto the main form. Or could you in the main form, call an iframe - which then deposits the result into the main form?

"Never underestimate the power of determination"

Stuart
Net+
 
sorry for the delay.

it's been one of those days.

you need to create a function:

Code:
<html>
<head><title>Search Xml</title>
<script language="text/javascript">
<!--
var nH = new ActiveXObject("Microsoft.XMLDOM")
nH.async="false"
nH.load("postcode.xml")

function searchXML(x){
// xml searches are case sensitive
// to overcome this we just convert all to lowercase
x=x.tolowercase;
strSearch="contains(translate(@fldZip, 'ABCDEFGHIJKLMNOPSQRTUVWXYZ', 'abcdefghijklmnopsqrtuvwxyz'), '"+ x +"')" ;

// run xpath query
var nH=tXML.selectNodes("/xml/rs:data/z:row["+ strSearch +"]");

// if nH.length is more than zero we have a result
if(nH.length>0)
 document.forms[0].elements['strResult'].value = nH.item(i).attributes.getNamedItem("fldZip").value
else
 alert("No Records Found")

}
//-->
</script>
</head>
<body>
<form>
<input type="text" name="strSearch"><br>
<input type="button" name="btnSearch" value="Search" onClick="searchXML(this.form.elements['strSearch'].value)"><br>
<input type="text" name="strResult">
</body>
</html>

Please bear in mind this is 'Friday' code - but it looks good.

hth

Simon

 
Stuart,
zipLookup.asp (the second piece of code I posted) would take care of setting the values in the main form when it loads by using the parent object. You would have to make your ASP populate the variables "city" and "state" with the correct values before this JavaScript loads.

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Thank you Simon,

I cut and pasted it in exactly as you put.

It is erroring on

Code:
<input type="button" name="btnSearch" value="Search" onClick="searchXML(this.form.elements['strSearch'].value)">

Saying object expected and pointing to that line.

Couple of additional questions. I'm guessing once it gets working, we can have the state and city fields auto fill right?

And would it also be possible to do a similiar onKey Up action to trigger the XML?

Thank you for your time

"Never underestimate the power of determination"

Stuart
Net+
 
Adam,

I will try to go with the XML method for my zip codes - but your idea opens up a possibility in an entirely different area of my application than this one - one that is not too feasable to put in any array.

Your reference for ("formName") the main form? I think i read it that the Iframe was wrapped in its own form.

"Never underestimate the power of determination"

Stuart
Net+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top