/    Sign up×
Community /Pin to ProfileBookmark

Populating drop down from xml file

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>[/CODE]

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>[/CODE]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @krug spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...