Lost Password?


  #1 (permalink)  
Old 09-23-2007, 11:15 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,428
Last Blog:
Google Web Toolkit
Credits: 1,208
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default AJAX: Intro

AJAX is one of the newest and post popular technologies. AJAX [Asynchronous JavaScript and XML] allows you to send a request to the server and view the results without having to refresh the page. This enables you to create a more "windows-like" applications. This tutorial will explain the basics of AJAX.

The heart behind AJAX is the JavaScript XMLHttpRequest object. Since all browsers react differently to JavaScript creating a cross browser application can be difficult. The core code I always use is below:

JavaScript Code:
  1. <script type="text/javascript">
  2. function ajax()
  3.   {
  4.   var xmlHttp;
  5.   try
  6.     {
  7.     xmlHttp=new XMLHttpRequest();
  8.     }
  9.   catch (e)
  10.     {
  11.     try
  12.       {
  13.       xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  14.       }
  15.     catch (e)
  16.       {
  17.       try
  18.         {
  19.         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  20.         }
  21.       catch (e)
  22.         {
  23.         alert("AJAX is not supported by your browser.");
  24.         return false;
  25.         }
  26.       }
  27.     }
  28.   }
  29. </script>

This code works with FireFox, Internet Explorer, Opera, and many more. To send information to a server, I use the open() and send() functions. Open accepts three parameters, the "connection" type (either POST or GET), the file location, and a boolean value which determines whether the connection should be asynchronous. The send() function "sends" the request. The code will look like this:

JavaScript Code:
  1. xmlHttp.open("GET", "ajax.php", true);
  2. xmlHttp.send(null);


Now before you actually submit data to a file you should actually make sure data needs to be submitted, and we do that by making sure the "state" was changed.

Which might look like this:

JavaScript Code:
  1. xmlHttp.onreadystatechange=function()
  2.       {
  3.       if(xmlHttp.readyState==4)
  4.         {
  5.         document.formName.elementName.value=xmlHttp.responseText;
  6.         }
  7.       }
  8.     xmlHttp.open("GET", "ajax.php", true);
  9.     xmlHttp.send(null);
  10.   }


The ready state has five "states": 0, 1, 2, 3, and 4. Ill tell you more about them in future tutorials when they are needed, but for this tutorial all we are concerned with is state four, which means the request is complete. So essentially what the code above does, is - if the request was complete, the web page is going to update the, lets say input box, named elementName inside the form named formName with the response from the server - which is a value echoed plain text. For example if ajax.php had this code:

PHP Code:
echo date('l dS \of F Y h:i:s A'); 
And you had an html file like this:

HTML4Strict Code:
  1. <title>Ajax Time</title>
  2. <script type="text/javascript">
  3. function ajax()
  4.   {
  5.   var xmlHttp;
  6.   try
  7.     {
  8.     xmlHttp=new XMLHttpRequest();
  9.     }
  10.   catch (e)
  11.     {
  12.     try
  13.       {
  14.       xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  15.       }
  16.     catch (e)
  17.       {
  18.       try
  19.         {
  20.         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  21.         }
  22.       catch (e)
  23.         {
  24.         alert("Ajax is not supported by your browser.");
  25.         return false;
  26.         }
  27.       }
  28.     }
  29.      xmlHttp.onreadystatechange=function()
  30.       {
  31.       if(xmlHttp.readyState==4)
  32.         {
  33.         document.formName.elementName.value=xmlHttp.responseText;
  34.         }
  35.       }
  36.     xmlHttp.open("GET", "ajax.php", true);
  37.     xmlHttp.send(null);
  38.   }
  39. </script>
  40. </head>
  41. <form name="formName">
  42. Anything: <input type="text"
  43. onkeyup="ajax();" name="anything" />
  44. Time: <input type="text" name="elementName" />
  45. </form>
  46. </html>

Assuming you setup everything properly, each time you type something in the "anything" input box the "elementName" input box should update with the current time.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall

Last edited by John; 09-23-2007 at 11:24 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 11-07-2007, 09:52 PM
Crane's Avatar   
Crane Crane is offline
Programming Expert
 
Join Date: Nov 2005
Posts: 399
Credits: 9
Rep Power: 14
Crane is on a distinguished road
Default

Excellent Tutorial. I always thought that AJAX was a new language (a combination of languages) but it doesn't seem so complicated. If you can create an XML document with one language all you need to do is fetch it with JavaScript.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Considering Ajax, Part 1: Cut through the hype Ronin JavaScript and CSS 0 12-13-2006 03:36 PM
Myth-Busting AJAX (In)security Jordan AJAX 1 12-03-2006 02:21 PM
AJAX Evolved Jordan JavaScript and CSS 0 09-09-2006 02:12 PM
More AJAX Resources NeedHelp JavaScript and CSS 2 07-13-2006 01:57 PM
Anybody use Ajax? Dan JavaScript and CSS 9 07-05-2006 04:25 PM


All times are GMT -5. The time now is 11:50 AM.

Contest Stats

WingedPanther ........ 2672.3
Xav ........ 2581.51
Brandon W ........ 1699.26
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 959.05
dcs ........ 650.52
Steve.L ........ 475.59
orjan ........ 409.29
chili5 ........ 380.6

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 97%

Ads