Jump to content

Opening an AJAX element inside an AJAX element?


frankhaugen

Recommended Posts

I'm new to AJAX so forgive me if I don't no the lingo fully.

 

I am working on a website, I have turned to AJAX from iframes, and I've hit a snag, which maybe some of you can help me with. Using this code:

<script class="AJAXscript" type="text/javascript">
function loadXMLDoc2()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
	}
  }
xmlhttp.open("GET","test.php",true);
xmlhttp.send();
}
</script>

<button type="button" onclick="loadXMLDoc2()">hi</button>

I open a php file, which in turn have links I need to use, using the "AJAX div element" it is inside of to present the second php document, but using the same code, of course with different names, it just isn't responsive. Any ideas??

 

 

Thanks for any help!!

 

 

here are code in my test project I'm using to figure this out:

 

index.php:

<!DOCTYPE HTML>
<html>
<head>
	<title>
		Lorem ipsum dolor sit amet
	</title>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<script type="text/javascript">
		function loadXMLDoc()
		{
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
			document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
			}
		  }
		xmlhttp.open("GET","test2.php",true);
		xmlhttp.send();
		}
	</script>
</head>
<body>
	<div id="wrapper">
		<div id="banner">

		</div>
		<div>
			<button type="button" onclick="loadXMLDoc()">Change Content</button>
		</div>
		<div>
			<div id="myDiv"><h2>Let AJAX change this text</h2></div>

		</div>
		<div></div>
		<div></div>
	</div>
</body>
</html>

 

test2.php:

<script class="AJAXscript" type="text/javascript">
function loadXMLDoc2()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
	}
  }
xmlhttp.open("GET","test3.php",true);
xmlhttp.send();
}
</script>

<button type="button" onclick="loadXMLDoc2()">hi</button>

 

test3.php:

<?php
echo "success";
?>

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.