PHPEveryday.com PHP and Web Development Tutorial
What are you looking for?

SMS : Line Codes Send Simple SMS (Part 2) »

AJAX - Client Side: Making Asynchronous Calls with XMLHttpRequest


AJAX Client Side Step By Step Tutorial - part 9: Ok, now, we will combine codes. We can see how they work. First, create a file named "ajaxclient.html" within www/test/ajax. Enter following code:
<!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","test.txt", 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
	  {
	    response = xmlHttp.responseText;
		myDiv.innerHTML += "Request status: 4 (complete). Server said: <br/>";
		myDiv.innerHTML += response;
	  }
	  catch(e)
	  {
	    alert("Error reading the response: " + e.toString());
	  }
	}
	else
	{
	  alert("Problem retrieving data:\n" + xmlHttp.statusText);
	}
  
  
  }
  
}
</script>
</head>

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

In this practice, we want to read data from server. For that, we create a file named "test.txt". Place within www/test/ajax. Enter some text like:

Hi, this is from server

Point your browser to http://localhost/test/ajax/ajaxclient.html. It must look from localhost not from local (double click from location directly).



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


Tag: Ajax, Client side, XMLHttpRequest, Asynchronous Calls Category: Post : March 17th 2008 Read: 1,991 Bookmark and Share

blog comments powered by Disqus


Database Tutorial
  • Learn PHP MySQL
  • Learn PHP ADOdb
  • Learn PHP Data Object/PDO
  • Learn PHP XML
  • Learn PHP SimpleXML
Security Tutorial
  • Learn PHP Security
  • Learn HTTP Authentication
  • Learn PHPSecureSite
Framework Tutorial
  • Learn CodeIgniter
  • Learn Joomla
  • Learn Smarty
  • Learn Zend Framework
Template Tutorial
  • Learn Joomla Template
  • Learn WordPress Template
API Tutorial
  • Learn Facebook
JS Framework Tutorial
  • Learn MooTools
  • Learn JQuery
AJAX Tutorial
  • Learn AJAX in 10 Minutes
  • Learn AJAX Client Side
  • Learn AJAX PHP
  • Learn AJAX Remote Server
  • Learn AJAX Repetitive
  • Learn AJAX MySQL
  • Learn AJAX Grid
Web Services Tutorial
  • Learn Web Services NuSOAP
  • Learn Web Services WSDL
  • Learn Web Services WSDL Array
  • Learn Web Services .NET Grid
  • Learn Web Services WDDX
Package Post
  • Joomla Intro
  • Joomla Component
  • Joomla Module
  • Joomla MVC
  • Joomla MVC Backend
  • PostNuke Intro
  • Zend Framework Intro
  • Zend Framework Action
  • Zend Framework Database
  • Zend Framework Registry
  • Zend Framework Config
  • Zend Framework Login
  • Zend Framework Session
  • PHP Array Tips
  • PHP File Tips
  • PHP Email
  • PHP Ms Excel
  • PHP Pattern
  • PHP SMS
  • Flash Database
  • PHP Multitier
  • jQuery Introduction
  • jQuery Selectors
  • Portable Web Server
  • Web Mobile Intro
  • Drupal Installation
  • Drupal Configuration