Thursday, July 2, 2009

PHP and AJAX

In this article i would like to explain little bit about AJAX and how it works with PHP.

What is AJAX?

AJAX(Asynchronus JavaScript and XML) is based on JavaScript and HTTP Request.

With AJAX JavaScript can communicate directly with the server, with theXMLHttpRequestobject. With this object, a JavaScript can fetch 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 requried data(information) from the server instead of whole pages.

What is HTTP Request ?

HTTP (Hypertext Transfer Protocol) is a request/response protocol,Whenever web browser fetches a page (date) from a web server, it does so using HTTP. This means your web browser sends a request for some data and the web server sends back a response using the HTTP.

What is XMLHttpRequest ?

XMLHttpRequest is a JavaScript object that was designed by Microsoft,The XMLHttpRequest object provides a way to communicate with a server after a web page has loaded, it's support all the major browaer like Internet Explorer, Firefox, safari.

After explaining the above important terms let see how PHP works with the AJAX
PHP and AJAX :
working with PHP and Ajax is very simple

Step I: we need to know how to create an XMLHTTPRequest object. The process differs slightly depending on whether you are using Internet Explorer (5+) with ActiveX enabled, or a standards-compliant browser such as Mozilla Firefox.

With IE5 and IE6 , the request like:

http = new ActiveXObject("Microsoft.XMLHTTP");

With IE7+, Firefox, Chrome, Opera, Safari.we can instantiate the object directly like:

http = new XMLHttpRequest();

Step 2: We need to write an event handler which will be called via some event on our page, and will handle sending our request for data to our server.
The event handler will use various methods of our XMLHTTPRequest object to:

(i) make the request of the server
(ii) check when the server says that it has completed the request
(iii) deal with the information returned by the server

using above two step you can write PHP application using AJAX.

1 comment:

Shekhar said...

Please provide some code samples