Jump to content

Help With Dropdown List / Ajax And Js


Guest I.odine
 Share

Recommended Posts

Guest I.odine

Hey Guys - I am trying to figure this out, but its doing my head in...I basically have followed this: http://www.w3schools.com/Ajax/ajax_xmlfile.aspto get a drop down list to populate from an XMl file, and when an option is selected a run down of all the nodes in that set are printed. works fine!Now what I am trying to do is...when an option is selected from the DDL instead of just response.write() i was hoping to have it fill in the values to some textboxes i have in my form already on the page. I am having a hard time though, as the final step that seems to do the actual writing is a seperate .asp page, and i have NO idea how to get it to access the elements on the form...PHP is not an option at this time, so asp seems to be all I can use.below is the code: (all available @ the link as well)code for drop down list that calls the fuction in selectposting.js

positions.Attributes.Add("onchange", "showjob(this.options[this.selectedIndex].value)")

Selectposting.js - function for dropdownlist "onChange"

var xmlhttp    function showjob(str)  {  xmlhttp=GetXmlHttpObject();  if (xmlhttp==null)    {    alert ("Your browser does not support AJAX!");    return;    }  var url="getjob.asp";  url=url+"?q="+str;  url=url+"&sid="+Math.random();  xmlhttp.onreadystatechange=stateChanged;  xmlhttp.open("GET",url,true);  xmlhttp.send(null);  }    function stateChanged()  {  if (xmlhttp.readyState==4)  {  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;  }  }    function GetXmlHttpObject()  {  if (window.XMLHttpRequest)    {    // code for IE7+, Firefox, Chrome, Opera, Safari    return new XMLHttpRequest();    }  if (window.ActiveXObject)    {    // code for IE6, IE5    return new ActiveXObject("Microsoft.XMLHTTP");    }  return null;  }

getjob.asp

<%  response.expires=-1  q=request.querystring("q")    set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")  xmlDoc.async="false"  xmlDoc.load(Server.MapPath("availablepositions.xml"))    set nodes=xmlDoc.selectNodes("rss/channel/item[title='" & q & "']")      for each x in nodes    for each y in x.childnodes  	response.write("<b>" & y.nodename & ":</b> ")  	response.write(y.text)  	response.write("<br />")    next  next  %>

So if anyone knows how i could do this...i would be VERY appreciative!Thank you in advance!-][-

Link to comment
Share on other sites

If you want to return several values and put them in various places, you'll need to change the ASP script to print the list of values, and change the Javascript to find the values and update the items on the page. So the ASP script will need to output the values in a list, which means each item needs to be separated. You could use XML to have it output the items in an XML structure, or you could use JSON to have it write the items in a JSON structure, or just separate each item with a character delimiter. The last one is the easiest but the least flexible, but if you use "|" as a delimiter, this is how you would write out the list of node text:

for each x in nodes    for each y in x.childnodes  	response.write(y.nodename & "|")    nextnext

In the stateChanged function in Javascript, instead of just writing the response text to the innerHTML property of the element, you would split the response text up into an array, and then write the individual array items where you want them to go.

var items = xmlhttp.responseText.split("|");for (var i = 0; i < items.length; i++){  alert(items[i]);}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...