Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

AJAX: Intro

ajax

  • Please log in to reply
1 reply to this topic

#1 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 23 September 2007 - 07:15 PM

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:

<script type="text/javascript">
function ajax()
{
var xmlHttp;
try
{
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("AJAX is not supported by your browser.");
return false;
}
}
}
}
</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:

xmlHttp.open("GET", "ajax.php", true);
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:

xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.formName.elementName.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET", "ajax.php", true);
xmlHttp.send(null);
}



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:

echo date('l dS \of F Y h:i:s A');


And you had an html file like this:

<html>
<head>
<title>Ajax Time</title>
<script type="text/javascript">
function ajax()
{
var xmlHttp;
try
{
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Ajax is not supported by your browser.");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.formName.elementName.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET", "ajax.php", true);
xmlHttp.send(null);
}
</script>
</head>
<body>
<form name="formName">
Anything: <input type="text"
onkeyup="ajax();" name="anything" />
Time: <input type="text" name="elementName" />
</form>
</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.
  • 0

#2 Crane

Crane

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 388 posts

Posted 07 November 2007 - 06:52 PM

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.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download