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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.