Navigation Search Other Links

TN38 proudly recommends cheap website hosting for small businesses at Pacifica Hosting. Visit UK website hosting for more information.

For web marketing training, presentations and workshops visit Pacifica Training. If you wish to ask questions about website marketing please head over and register free at the website marketing forum, a knowledge base and community driven discussion group.

What is AJAX?

AJAX isn't new; it's a collection of technologies that have been brought together to achieve the goal of dynamically loading data from a server and updating the page without refresh. You may be more familiar with AJAX due to Googles' Suggest feature. If you're UK based, you may recognise the name from the AJAX multi-purpose cleaner we all used to buy.

AJAX stands for Asynchronous JavaScript and XML. It incorporates standard HTML for output to the client window, window interaction using the DOM, data interchange using XML, asynchronous data retrieval using XMLHttpRequest and JavaScript which glues the technologies togther.

Glad we cleared that up.

What can AJAX do?

The technologies have always been there but using AJAX as a foundation, we are getting nearer to the levels of power and interactivity of applications without going down the route of Java or ActiveX. Fantastic!

Think of real-time newsfeeds, Formula 1 lap times, chat and many, many more; the list is endless, all this without the need for third party objects and all based on the web standards which makes it far more accessible to people and devices.

Could the typical business application be consumed by the internet?

The problem with AJAX

AJAX requires a certain level of ability in terms of the users computer and software used. JavaScript is required which isn't always supported, the difference between Internet Explorer [bless it] and all the other browsers adds complexity to the code. As we all know, complexity = inaccessibility/unreliability.

JavaScript has served us well in terms of form validation and other usability enhancements but I've never found the need to really dig deep into client-side script due to most things disagreeing with it such as search engines and assistive devices.

There is also a big usability shift as well. We all know the internet, it's reponse to form inputs, navigation, even tables and frames. Now however, we are having to deal with a new model, the internet is reinventing itself and becoming more powerful which raises the game. I still meet new clients who haven't ever been online. Some use the internet daily but are petrified of being presented with more than two choices when making a decision.

Is it worth me learning about AJAX?

Almost certainly. It's powerful and if you wish to operate at a professional level, even knowing the fundamental underlying technologies is essential. If ever there was a case for dropping the server-side stuff for a few months and learning JavaScript in depth, this has to be it.

What does AJAX look like?

The HTML needed for the example looks like:

<form name="form_select">
  <select name="select_select"
        onChange="getInfo()">
    <option value="" selected="selected">
      Select fruit
    </option>
    <option value="1">
      Apples
    </option>
    <option value="2">
      Pears
    </option>
  </select>
</form>
<div id="dyn_div">
  Please select a fruit.
</div>

The JavaScript needed to call the server and update the page looks like:

function createRequestObject(){
  var request_;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer"){
    request_ = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else
  {
    request_ = new XMLHttpRequest();
  }
  return request_;
}
var http = createRequestObject();
function getInfo(){
  http.open('get', 'test.php?id='
    + document.form_select.select_select.selectedIndex);
  http.onreadystatechange = handleInfo;
  http.send(null);
}
function handleInfo(){
  if(http.readyState == 1){
    document.getElementById('dyn_div').innerHTML = 'Loading...';
  }
  if(http.readyState == 4){
    var response = http.responseText;
    document.getElementById('dyn_div').innerHTML = response;
  }
}

And the server-side code, using PHP in this example looks like:

<?php
  if( $_GET['id'] == 1 )
  {
    echo 'You chose apples';
  }
  else
  {
    echo 'You chose pears';
  }
?>

Not immediately impressive but no refresh despite a server call! The interactive possibilities :)

Useful?

Assistance?

Comments

Jim says:

If I convert your simple PHP script to ASP and try this code, I get problems with the page creating "duplicate" form controls. It works, but the form controls are duplicated.

Is there any way to correct this?

Ed says:

Hi Jim,

The server script shouldn't make a difference to the form controls. The only prerequisite of the script is to return a string.

An ASP equivalent would be:

<%
if request.querystring("id")="1" then
response.write("You chose apples")
else
response.write("You chose pears")
end if
%>

Email me or post a link to the script if it's online and I'll take a look...

Jim says:

Hey Ed,

Thanks for the reply. I think the problem is inside
the "document.getElementById('dyn_div').innerHTML = http.responseText" statement.

responseText is getting the entire source code of the page and is being redisplayed inside the DIV as HTML, hence creating a duplicate form control.

It works, the correct message is displayed, but the form control is also redisplayed.

Am I missing something in the Content-Type?

I took your PHP code example and recreated the page on my personal site (link below). I'm not sure where it's breaking.

http://www.dialwforwarp.com/jim/test.php

Ed says:

You need to seperate the HTML page with the form controls from the PHP page with the data.

Because the JavaScript is calling 'test.php' which is the page with the form controls and the .responseText is looking for output, it's pulling the form as well as the PHP data as every character is considered output.

It's this causing the problem. Hope that helps.

Jim says:

You know, I should have thought of that! Just too many projects and a company network on the fritz. Just one of those days. ;-)

Works perfectly now. Thanks much for the help!

Assistance?

Post a comment

To reproduce, read the Copyright Notice
Published by Edward Clarke
Powered by Movable Type