Jump to content

Can ajax do this?


amalosoul

Recommended Posts

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)...

Link to comment
https://forums.phpfreaks.com/topic/41169-can-ajax-do-this/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/41169-can-ajax-do-this/#findComment-202753
Share on other sites

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"
?>

Link to comment
https://forums.phpfreaks.com/topic/41169-can-ajax-do-this/#findComment-206040
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.