"Lit the Candle"

PHP and Ajax for Beginners

AJAX is not a new programming language,it is a type of programming made popular by Google (with Google Suggest) to use existing standards based on JavaScript and HTTP requests.

With AJAX, your JavaScript can communicate directly with the server, using the JavaScript XMLHttpRequest object. With this object, your JavaScript can trade data with a web server, without reloading the page.

AJAX uses asynchronous data transfer (HTTP requests) between the browser and the web server, allowing web pages to request small bits of information from the server instead of whole pages.

The AJAX technique makes Internet applications smaller, faster and more user-friendly.

AJAX is a browser technology independent of web server software.

AJAX is based on the following web standards:

  • JavaScript
  • XML
  • HTML
  • CSS

There is nothing new to learn.AJAX is based on existing standards. These standards have been used by most developers for several years.The keystone of AJAX is the XMLHttpRequest object.

Different browsers use different methods to create the XMLHttpRequest object.

Internet Explorer uses an ActiveXObject, while other browsers uses the built-in JavaScript object called XMLHttpRequest.How the browsers work with ajax its depends these objects.

There are  three important properties of the XMLHttpRequest object.

a) The onreadystatechange Property

b) The readystate Property

c) The responseText Property

After a request to the server, we need a function that can receive the data that is returned by the server.

The onreadystatechange property stores your function that will process the response from a server. This is not a method, the function is stored in the property to be called automatically.

To send off a request to the server, we use the open() method and the send() method.

The open() method takes three arguments. The first argument defines which method to use when sending the request (GET or POST). The second argument specifies the URL of the server-side script. The third argument specifies that the request should be handled asynchronously. The send() method sends the request off to the server. If we assume that the HTML and PHP file are in the same directory, the code would be:

xmlHttp.open("GET","sample.php",true);
xmlHttp.send(null);

For a  example of php and ajax you should have a look at this link.

While responseText returns the HTTP response as a string, responseXML returns the response as XML.

The ResponseXML property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties.

As an example of Ajax ResponseXML you can try the this one.

By this time i hope that you got the very basic of ajax functionality. Now its your time to play more with Ajax.

March 23, 2009 Posted by milansaha | Ajax, Uncategorized | | No Comments Yet