intelliproject logo

Location: Web development - Ajax & Atlas    License: The Intelliproject Open License (IPOL)

Using Ajax with Javascript and PHP

Posted by Silviu Caragea

In this article we'll discuss the basic principles of remote scripting using Ajax, (asynchronous JavaScript and XML) to allow web pages to be updated without the user having to wait for a page refresh.

Skill: Beginner

Posted: 18/10/2008

Views: 482

Rating: 5.00 /5

Popularity: 1.51

Sign Up to vote for this article

Introduction

In this article we'll discuss the basic principles of remote scripting using Ajax, (asynchronous JavaScript and XML) to allow web pages to be updated without the user having to wait for a page refresh. 

How does it work?

Wait server to complete the request.

loading.jpg

Display the request response .

frame_completed.jpg

Ajax script makes a request of a server by using XMLHTTPRequest object. XMLHttpRequest (XHR) is a DOM API that can be used by JavaScript and other web browser scripting languages to transfer XML and other text data between a web server and a browser.

There are some relatively simple steps to use Ajax in your application.

1. Create an XMLHTTPRequest object .This process differs slightly depending on what you are using: Internet Explorer (5+) with ActiveX, or a standards-compliant browser like Firefox.

  • In IE, the request looks like:request = new ActiveXObject('Msxml2.XMLHTTP');
  • In a standards-compliant browser we can instantiate the object directly:request = new XMLHttpRequest();

The following javascript function create your XMLHTTPRequest object for all supported browsers:

2. We need to write an event handler which will be called via some event on your webpage, and will handle sending our request for data to our server.
We can make our request of the server by using a GET method to an appropriate server-side script.


Note that the function listens to the onreadystatechange property of the XMLHTTPRequest object and, each time this parameter changes, calls a further anonymous function.

These functions establish when the server has completed our request, and do something useful with the data it has returned. In this case, we have received our information as simple html text via the responseText property.  

License

This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)

About the author

Silviu Caragea

Silviu Caragea is the Founder, Administrator and Chief Editor who wrote and runs The IntelliProject.

He's been programming since 2000 and now he's student at The Faculty of Economic Cybernetics, Statistics and Informatics from Bucharest. In the same time he's working as software developer at Cratima Software, a Romanian software and web design company that activates both on the local and foreign market, providing its customers with software development services, internet and intranet solutions, web design, graphic design and IT consultancy.

His programming experience includes:
- C,C++, Visual C++(Win32 API, MFC, ADO, STL, DAO, ODBC, ATL, COM, DirectShow, DirectDraw, WTL)
- Open Source libraries :CURL & Boost
- HTML, CSS
- Java (SE,ME)
- JavaScript, Ajax, Google Web Toolkit (GWT)
- Php, MySQL
-Oracle, PL SQL
- C# .NET
-Objective C, IPhone SDK, Cocoa

Location: Romania
Ocupation: Software Engineer
Home page: http://www.intelliproject.net

Posted by J. Pape at 16/12/2008 08:04
The biggest speed issue with using AJAX, Javascript and PHP is that you cant easely transport arrays. so you have to transport the entire makup...

i found a way to serialise the array in PHP and desirialise it in Javascript.
Posted by Jason Houle at 02/01/2009 15:08
Why not just use the json_encode (http://www.php.net/json_encode) and decode functions instead of serializing a php array?

Sign up to post message on the article message board!