phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


AJAX - Client Side: Simple Application for Process XML data

Tag:    Category:
post: 17 Mar 2008 read: 528


AJAX Client Side Step By Step Tutorial - part 12: This post is last article from this series. We see how combine all code that we learned before.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Client</title>
<script type="text/javascript">

var xmlHttp = createXmlHttpRequestObject();
// creates XMLHttpRequest Instance
function createXmlHttpRequestObject(){

  // will store XMLHttpRequest object
  // at here
  var xmlHttp;

  // works all exceprt IE6 and older  
  try
  {
  
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();    
  }
  catch(e)
  {
    // assume IE 6 or older
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e){ }
  }
  
  // return object or display error
  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()
{
  myDiv = document.getElementById("myDivElement");

  if(xmlHttp.readyState == 1)
  {
    myDiv.innerHTML += "Request status: 1 (loading) <br/>";
  }
  else if (xmlHttp.readyState == 2)
  {
    myDiv.innerHTML += "Request status: 2 (loaded) <br/>";
  }
  else if (xmlHttp.readyState == 3)
  {
    myDiv.innerHTML += "Request status: 3 (interactive) <br/>";	
  }
  else if (xmlHttp.readyState == 4)
  {
    if(xmlHttp.status == 200)
	{
	  try
	  {
	    handleXMLData();
	  }
	  catch(e)
	  {
	    alert("Error reading the response: " + e.toString());
	  }
	}
	else
	{
	  alert("Problem retrieving data:\n" + xmlHttp.statusText);
	}
  
  
  }
  
}

function handleXMLData()
{
  var xmlResponse = xmlHttp.responseXML;
  
  xmlRoot = xmlResponse.documentElement;

  idArray = xmlRoot.getElementsByTagName("id");  
  titleArray = xmlRoot.getElementsByTagName("title");
  authorArray = xmlRoot.getElementsByTagName("author");   
  
  var html = "";
  
  for( var i=0; i<titleArray.length; i++)
  {
    html += idArray.item(i).firstChild.data + ", " + titleArray.item(i).firstChild.data +  ", " + authorArray.item(i).firstChild.data + "<br/>";
  }
  myDiv = document.getElementById("myDivElement");
  myDiv.innerHTML += "Server says: <br/>" + html;  
}
</script>
</head>

<body onload="process()">
Our collections:
<div id="myDivElement" />
</body>
</html>

Point your browser to http://localhost/test/ajax/ajaxclient.php. You will get like this:

ajax client side handle xml data


Series this article:
AJAX - Client Side: Techniques Introduction
AJAX - Client Side: JavaScript Overview
AJAX - Client Side: JavaScript and Document Object Model
AJAX - Client Side: JavaScript Events and the DOM
AJAX - Client Side: JavaScript, DOM, and CSS
AJAX - Client: XMLHttpRequest Object
AJAX - Client Side: Initiating Server Request Using XMLHttpRequest
AJAX - Client Side: Handling Server Response
AJAX - Client Side: Making Asynchronous Calls with XMLHttpRequest
AJAX - Client Side: Working with XML Structure
AJAX - Client Side: Processing XML Data use XMLHttpRequest
AJAX - Client Side: Simple Application for Process XML data

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting