amalosoul Posted March 4, 2007 Share Posted March 4, 2007 I want to load several pages from the server at the same time by using ajax can this be done? I have two divs and in each one of them I want to load a different page...I have tried something like this (see the code below) but it only loads one page (the last one...) Thank you in anticipation! url- page url nid- id of the div (top or down) the url is taken from a form... <script> i=0 function loadXMLDoc(url,nid) { id=nid xmlhttp=new Array() xmlhttp[i]=null // code for Mozilla, etc. if (window.XMLHttpRequest) { xmlhttp[i]=new XMLHttpRequest() } // code for IE else if (window.ActiveXObject) { xmlhttp[i]=new ActiveXObject("Microsoft.XMLHTTP") } if (xmlhttp!=null) { xmlhttp[i].onreadystatechange=state_Change xmlhttp[i].open("GET",url,true) xmlhttp[i].send(null) } else { alert("Your browser does not support XMLHTTP.") } } function state_Change() { // if xmlhttp shows "loaded" if (xmlhttp[i].readyState==4) { // if "OK" if (xmlhttp[i].status==200) { document.getElementById(id).innerHTML=xmlhttp[i].responseText i++} else { alert("Problem retrieving data:" + xmlhttp[i].statusText) } } } function loading1() { if (document.myform.top.value!=null) loadXMLDoc(document.myform.top.value,"top") loading2() } function loading2() { if (document.myform.down.value!=null) loadXMLDoc(document.myform.down.value,"down") } </script> P.S: I have tried with xmlhttp as a simple variable (not an array) but it still does not work, the result is the same(it loads one the second page)... Quote Link to comment Share on other sites More sharing options...
JakeTheSnake3.0 Posted March 8, 2007 Share Posted March 8, 2007 Perhaps my concept of an IF structure is lacking in complexity, but shouldn't if (document.myform.top.value!=null) loadXMLDoc(document.myform.top.value,"top") be if (document.myform.top.value!=null) { loadXMLDoc(document.myform.top.value,"top"); } either way, you can also do this another way...instead of hard-coding your....code, you can accept your 1st and 2nd parameters in your main function as arrays....and then do a for loop to run your ajax functions instead of having to sequentially load your documents using IF statements.... "outside code" urlArray = new array('http://www.mydomain.com/page1','http://www.mydomain.com/page2'); idArray = new array('div1','div2'); loadXMLDoc(urlArray,idArray); "ajax code" <script> function loadXMLDoc(url,nid) { var xmlhttp = new array(); for(i=0; i<url.length; i++) { // code for Mozilla, etc. if (window.XMLHttpRequest) { xmlhttp[i]=new XMLHttpRequest(); } else if (window.ActiveXObject) { // code for IE xmlhttp[i]=new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlhttp[i]!=null) { xmlhttp[i].onreadystatechange=state_Change(i,nid[i]); xmlhttp[i].open("GET",url[i],true); xmlhttp[i].send(null); } else { alert("Your browser does not support XMLHTTP."); } } } function state_Change(xmlhttpReference,divIdReference) { i=xmlhttpReference; id=divIdReference; // if xmlhttp[i] shows "loaded" if (xmlhttp[i].readyState==4) { // if "OK" if (xmlhttp[i].status==200) { document.getElementById(id).innerHTML=xmlhttp[i].responseText; } else { alert("Problem retrieving data:" + xmlhttp[i].statusText); } } } </script> Do you understand what I'm doing here? Quote Link to comment Share on other sites More sharing options...
amalosoul Posted March 8, 2007 Author Share Posted March 8, 2007 Thank you for your reply! I will put it into action and see what I get:)! Quote Link to comment Share on other sites More sharing options...
amalosoul Posted March 10, 2007 Author Share Posted March 10, 2007 Nope it does not work:(! Quote Link to comment Share on other sites More sharing options...
JakeTheSnake3.0 Posted March 11, 2007 Share Posted March 11, 2007 I didn't give you an example that *will* work - I gave you the logic behind HOW to go about doing it. The correct syntax/functions are up to you. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted March 13, 2007 Share Posted March 13, 2007 The body of an if statement can only be a single program statement. Wrapping multiple program statements inside of curly braces creates a statement block, which is itself a statement. This means that if the body of your if, while, or for loop is a single statement, the curly braces are optional. However, I strongly recommend always adding the curly braces. It's very easy to add something to the body of an if and forget to change it to include curly braces. Then there's always this: <?php if(true) if(false) echo "true"; else echo "Hello World" ?> Quote Link to comment 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.