Hello,
I’m trying to populate a drop down list with addresses from an xml file for a zip code entered by the user. However, I’m required to return multiple addresses for the zip code entered if there is more than one address available. I’m really not sure how to do this, and the searches I turned up on google haven’t been much help. Any help or tips would be appreciated.
This is my code so far:
[CODE]<head>
<script type=”text/javascript”>
/* <![CDATA[ */
var httpRequest = false;
function getRequestObject()
{
try
{
httpRequest = new XMLHttpRequest();
}
catch (requestError)
{
try
{
httpRequest = new ActiveXObject(“Msxm12.XMLHTTP”);
}
catch (requestError)
{
try
{
httpRequest = new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch (requestError)
{
window.alert(“Your browser does not support AJAX!”);
return false;
}
}
}
return httpRequest;
}
function updateAddress()
{
if (!httpRequest)
{
httpRequest = getRequestObject();
}
else
{
httpRequest.abort();
httpRequest.open(“get”, “address.xml”);
httpRequest.send(null);
httpRequest.onreadystatechange = getAddressInfo;
}
}
function getAddressInfo()
{
if (httpRequest.readyState == 4 && httpRequest.status == 200)
{
var adrs = httpRequest.responseXML;
var locations = adrs.getElementsByTagName(“Row”);
var option = document.forms[0].address.createElement(“option”)
var notFound = true;
for (var i = 0; i < locations.length; ++i)
{
if (document.forms[0].zip.value == adrs.getElementsByTagName(“ZIP_Code”)[i].childNodes[0].nodeValue)
{
option.text = adrs.getElementsByTagName(“Address”)[i].childNodes[0].nodeValue);
document.forms[0].address.options.add(option.text);
notFound = false;
}
}
if (notFound)
{
window.alert(“Zip code not found”);
}
}
}
/* ]]> */
</script>
</head>
<body>
<p>Zip Code:<br /><input type=”text” name=”zip” size=”5″ maxlength=”5″ value=”” onblur=”updateAddress()” /></p>
<p>Address:<select name=”address”>
<option>Select One</option>
</select></p>
</body>
</html>
And this is a portion of the xml file:
[CODE]<?xml version=”1.0″ encoding=”UTF-8″?>
<Import>
<Row>
<ZIP_Code>12345</ZIP_Code>
<Address>280 Hillside Drive</Address>
<Address>544 Old Mill Road</Address>
<Address>788 HighBridge DriveAddress>
<Address>902 Greenforest Road</Address>
<Address>134 Poland Ave</Address>
</Row>
<Row>
<ZIP_Code>01234</ZIP_Code>
<Address>9034 Tyson Street</Address>
</Row>
</Import>