I've read a couple of other threads on this but can't quite seem to grasp it. I don't really know a lot about Javascript and have basically guessed my way through so far. I have one function run an AJAX request to get some rows from a database. Then for each row, I need to run a nested AJAX request and return it's values to the first function. Both AJAX requests work independently however I don't know how to nest them correctly. Here is what I have:
function updateSummaryVariablesInput(typeId) {
if (typeId=='') {
document.getElementById('summaryVariables').innerHTML='';
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp2=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp2=new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp2.onreadystatechange=function() {
if (xmlhttp2.readyState==4 && xmlhttp2.status==200) {
xmlDoc=xmlhttp2.responseXML;
txt='<table>';
x=xmlDoc.getElementsByTagName('row');
for (i=0;i<x.length;i++) {
if (x[i].getElementsByTagName('common')[0].childNodes[0].nodeValue < 1) {
txt=txt + '<tr><th style=\"width: 150px;\">' + x[i].getElementsByTagName('label')[0].childNodes[0].nodeValue + '</th><td>';
// Select
if (x[i].getElementsByTagName('input')[0].childNodes[0].nodeValue == 'select') {
txt=txt + '<select name=\"' + x[i].getElementsByTagName('title')[0].childNodes[0].nodeValue + '\"><option></option>';
myoptions = getSummaryVariableOptions(x[i].getElementsByTagName('id')[0].childNodes[0].nodeValue);
//alert(myoptions.length);
for (j=0;j<myoptions.length;j++) {
txt=txt + '<option value=\"' + myoptions[j] + '\">' + myoptions[j] + '</option>';
}
txt=txt + '</select>';
}
// Text
if (x[i].getElementsByTagName('input')[0].childNodes[0].nodeValue == 'text') {
txt=txt + '<input type=\"text\" name=\"' + x[i].getElementsByTagName('title')[0].childNodes[0].nodeValue + '\" />';
}
txt=txt + '</td></tr>';
}
}
txt=txt + '</table>';
document.getElementById('summaryVariables').innerHTML=txt;
}
}
xmlhttp2.open('GET','/cgi/new/Ajax/getOutageVariablesByTypeId.php?typeId='+typeId,true);
xmlhttp2.send();
}
function getSummaryVariableOptions(variableId) {
var options = new Array();
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp3=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp3=new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp3.onreadystatechange=function() {
if (xmlhttp3.readyState==4 && xmlhttp3.status==200) {
xmlDoc=xmlhttp3.responseXML;
x=xmlDoc.getElementsByTagName('row');
for (i=0;i<x.length;i++) {
options[i] = x[i].getElementsByTagName('description')[0].childNodes[0].nodeValue;
}
alert(options.length);
}
}
xmlhttp3.open('GET','/cgi/new/Ajax/getOutageVariableOptionsByVariableId.php?variableId='+variableId,true);
xmlhttp3.send();
//alert(options.length);
return options;
}
Run as is, I will get an alert with a valid number, e.g. 5. If I uncomment any of the other alert functions they will only output 0.