i am reading book AJAX and PHP Building Responsive Applications..
I've tryed to run code from that book but it doesn't seem to work.. probably i am missing something but i checked a lot of times and seems like everything is correct, thou application doesn't work correct!

XML file

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
	<books>
		<book>
			<title>
				Building responsive Web Applications with AJAX and PHP
			</title>
			<isbn>
				1-904811-82-5
			</isbn>
		</book>
		<book>
			<title>
				Beginning PHP 5 and MySQL E-Commerce: From Novice to Professional
			</title>
			<isbn>
				1-59059-392-8
			</isbn>
		</book>
	</books>
</response>

html file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhmtl11/DTD/xhtml11.dtd">
<html>

<head>
	<title>AJAX Foundations: Javascript and XML</title>
	<script type="text/javascript" src="books.js"></script>
</head>

<body onload="process()">
	Server, tell me your favorite books!
	<br />
	<div id="myDivElement" />
</body>

</html>

Javascript file:

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
	var xmlHttp;
	
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
	
	if(!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

function process()
{
	if(xmlHttp)
	{
		try
		{
			xmlHttp.open("GET", "books.xml", true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		catch(e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}

function handleRequestStateChange()
{
	if(xmlHttp.readyState == 4)
	{
		if(xmlHttp.status == 200)
		{
			try
			{
				handleServerResponse();
			}
			catch(e)
			{
				alert("Error reading the response: " + e.toString());
			}
		}
		else
		{
			alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
		}
	}
}

function handleServerResponse()
{
	var xmlResponse = xmlHttp.responseXML;	
	xmlRoot = xmlResponse.documentElement;
	
	titleArray = xmlRoot.getElementByTagName("title");
	isbnArray = xmlRoot.getElementByTagName("isbn");
	
	var html = "";
	
	for(var i=0; i<titleArray.length i++)
		html += titleArray.item(i).firstChild.data + ", " + isbnArray.item(i).firstChild.data + "<br />";
		
	myDiv = document.getElementById("myDivElement");
		
	myDiv.innerHTML = "Server says: <br />" + html;
}

thanks

A couple of problems:

getElementByTagName should be: getElementsByTagName


You have a missing semicolon here:

for(var i=0; i<titleArray.length i++)

should be:

for(var i=0; i<titleArray.length; i++)

SoniaBaby

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.