shane07 Posted January 2, 2009 Share Posted January 2, 2009 When I run following through 4 different 'select' onchange event. It works well. But when I tried to run through a for loop only the last array element is processed. Is my concept with 'readystate' wrong? function fillnext(){ nextArray=new Array(); nextArray[0]='element1'; nextArray[1]='element2'; nextArray[2]='element3'; nextArray[3]='element4'; nextIdArray=new Array(); nextIdArray[0]='id1'; nextIdArray[1]='id2'; nextIdArray[2]='id3'; nextIdArray[3]='id4'; for(i=0;i<=3;i++){ fnext=nextArray[i]; fnextId=nextIdArray[i]; fillQuery(fnext,fnextId); } } function fillQuery(next,nextId){ present='something'; url='http://<?=$_SERVER['HTTP_HOST']."/"?>fillnext.php'; present=document.getElementById(present).value; var pageUrl = url+"?present="+present+"&next="+next if(window.ActiveXObject) { xmlRequest = new ActiveXObject("Microsoft.XMLHTTP"); } else { xmlRequest = new XMLHttpRequest(); } xmlRequest.open("GET", pageUrl, true); xmlRequest.send(null); xmlRequest.onreadystatechange=function(){ if(xmlRequest.readyState==4){ if (xmlRequest.status == 200) alert(nextId); alert(xmlRequest.responseText); } } } The 'alert(nextId)' alerts only the last element. But when I put 'alert(nextId)' at the top it alerts all the array elements Quote Link to comment https://forums.phpfreaks.com/topic/139240-xml-request-not-working-in-for-loop/ Share on other sites More sharing options...
corbin Posted January 2, 2009 Share Posted January 2, 2009 Somewhere before xmlRequest is used in your function, put: var xmlRequest; I suspect this is a problem with variable scope. Quote Link to comment https://forums.phpfreaks.com/topic/139240-xml-request-not-working-in-for-loop/#findComment-728331 Share on other sites More sharing options...
shane07 Posted January 3, 2009 Author Share Posted January 3, 2009 Somewhere before xmlRequest is used in your function, put: var xmlRequest; I suspect this is a problem with variable scope. Thank You very much. It was really the problem with variable scope. Now my code is working Quote Link to comment https://forums.phpfreaks.com/topic/139240-xml-request-not-working-in-for-loop/#findComment-728598 Share on other sites More sharing options...
corbin Posted January 3, 2009 Share Posted January 3, 2009 No problem ;p. Quote Link to comment https://forums.phpfreaks.com/topic/139240-xml-request-not-working-in-for-loop/#findComment-728721 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.