Jump to content

AJAX in loops


rhyspaterson

Recommended Posts

Hey guys,

 

Having trouble using a counter in a for loop with AJAX calls. Code below:

 

for(var x = 0; x < allPrinters.length; x++){ 
		$.ajax( {
		   url : "/data/printer_data[x].xml", dataType : ($.browser.msie) ? "text" : "xml", success : function(data) {
			  var xml; if (typeof data == "string") {
				 xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = false; xml.loadXML(data);
				 }
			  else {
				 xml = data;
				 }

				if(!xml.getElementsByTagName("array")){
					alert("NULL!");
				}

				// Returned data available in object "xml"
				var name = xml.getElementsByTagName("system.sysname.0")[0].childNodes[0].nodeValue;

				var type = xml.getElementsByTagName("25.3.2.1.3.1")[0].childNodes[0].nodeValue;
				type = removeDoubleQuotes(type);

				var consoleDisplay = xml.getElementsByTagName("43.16.5.1.2.1.1")[0].childNodes[0].nodeValue;
				consoleDisplay = removeDoubleQuotes(consoleDisplay);

					var row = document.createElement('TR');
					tmp.appendChild(row);

						var container = document.createElement('TD');
						// ----------------------------------------------------
						container.innerHTML= x;
						// ----------------------------------------------------
						row.appendChild(container);


						var container = document.createElement('TD');
						container.setAttribute('class', 'lft');
						container.innerHTML=name;
						row.appendChild(container);

						var container = document.createElement('TD');
						container.innerHTML=type;
						row.appendChild(container);	

						var container = document.createElement('TD');
						container.innerHTML=consoleDisplay;
						row.appendChild(container);											

						var container = document.createElement('TD');
						container.innerHTML='Undefined';
						row.appendChild(container);	

				document.getElementById('writeroot').appendChild(newTable);
				// Run the TableSort script
				fdTableSort.init('generatedTable');
			  }
		});	 						
	}

 

Say for example that allPrinters.length is 10.

 

I get the table rows generated the correct amount of times (10), however container.innerHTML= x is always set to 10 in each of the rows - instead of 1, 2, 3, 4 ect.. for each row generated. The x variable works for /data/printer_data(x).xml (i.e opening printer_data1.xml then printer_data2.xml ect)  but not for container.innerHTML= x.

 

Any suggestions?

Cheers,

Rhys

Link to comment
https://forums.phpfreaks.com/topic/125560-ajax-in-loops/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.