[advanced search]
Results from the most recent live auction are here.
23 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Webmaster Tutorials
User Name
Password

Old 10-22-2006, 06:59 PM   · #1
AbsoluteKC
NamePros Regular
 
AbsoluteKC's Avatar
 
Trader Rating: (12)
Join Date: Apr 2006
Posts: 691
NP$: 60.55 (Donate)
AbsoluteKC is a splendid one to beholdAbsoluteKC is a splendid one to beholdAbsoluteKC is a splendid one to beholdAbsoluteKC is a splendid one to beholdAbsoluteKC is a splendid one to beholdAbsoluteKC is a splendid one to behold
Easy Startup Guide for AJAX

Hi all,

I have been learning Ajax lately and found that this is actually not a difficult technique to dive in and it is very useful for adding new web2.0 fuctions to our current sites. I have compiled the following summary to help any new developers to head start their learning of AJAX.

What is AJAX

AJAX stands for Asynchronous JavaScript and XML. It is actually more of a technique as it utilize the XMLHttpRequest object in the common browsers.

What is AJAX used for

AJAX is mainly used to load a partial webpage to display updated content rather than the conventional GET / POST methods where the whole page is reloaded. The advantage in loading a partial section of a web page is that user can still see the other content of a page while the AJAX is working. Some common applications that utilize AJAX currently is Gmail and NetFlix.

An Easy Example

The following example illustrate a user press a button on the form and the server will response and display the details of the newest member.

1st step: Creating the XMLHttpRequest Object

The first step we do is to create the XMLHttpRequest Object (XHR) so that AJAX can work. The following codes are meant for creating this instance:
Code:
function GetXmlHttpObject() { var objXMLHttp=null if (window.XMLHttpRequest) { objXMLHttp=new XMLHttpRequest() } else if (window.ActiveXObject) { objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") } return objXMLHttp }

The above function will create the XHR. The reason why there is an if loop is that some browsers do not support ActiveX objects such as Firefox, as it implements XHR as a native JavaScript object. Therefore, the above codes will cover all cases.

2nd step: Coding the main function

Code:
function testajax() { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="pulldata.php" xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) }

The main function will first test if the browser supports XHR, if not, end execution. We have three new properties here namely, onreadystatechange, open and send. Allow me to explain the flow:

1. The variable url, contains the URL to the file that does the processing of the queries like database abstraction. In our case, the file is coded in PHP. You can use any server-side scripting you prefer

2. onreadytstatechange will be triggered based on the application status. For example, it will trigger stateChanged() function once it has finished loading or is loading. I will explain this in the next step.

3. open will send the queries to the URL. In our case, we use the GET method to pass to the URL as stated. The true value means that this request is asynchronously. True is the default value.

4. send simply sends the request to the server.

3rd step: Detailing the stateChanged function

Our stateChanged function is as follows:
Code:
function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") document.getElementById("show").innerHTML=xmlHttp.responseText if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") document.getElementById("show").innerHTML="<div align=center>Loading...</div>" }

We have two loops here. As I have mentioned before, this function will be triggered whenever the request returns a new status. In the above example, we will show the user a "Loading.." notification once the application starts loading. Upon completion of the whole request, it will display the results in the show container.

4th step: Finishing up with the Server Side

Our pulldata.php looks like:
PHP Code:
<?php
//select from database the latest records

//save the data into the result[]

//output the respective results
echo'The latest member is: '.$results["name"].' ';
?>

This whole string will be passed back to the client side through the responseText property and displayed in the page subsequently.

Final Step: Event triggerer

We will have to decide how are we going to intial this request. Is it through a form button? Or a link?
I have illustrated a trigger by a press on the button below:
Code:
<form action="#"> <input type="button" value="View the newest member" onclick="testajax();" /> </form>


Putting all together
In total, we have two files, our main page and the server processing file.

Main Page:
Code:
<html> <head></head> <script type="text/javascript"> var xmlHttp function GetXmlHttpObject() { var objXMLHttp=null if (window.XMLHttpRequest) { objXMLHttp=new XMLHttpRequest() } else if (window.ActiveXObject) { objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") } return objXMLHttp } function testajax() { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="pulldata.php" xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") document.getElementById("show").innerHTML=xmlHttp.responseText if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") document.getElementById("show").innerHTML="<div align=center>Loading...</div>" } </script> <body> <form action="#"> <input type="button" value="View the newest member" onclick="testajax();" /> </form> <div id="show"></div> </body></html>


pulldata.php
PHP Code:
<?php
//select from database the latest records

//save the data into the result[]

//output the respective results
echo'The latest member is: '.$results["name"].' ';
?>

You have just knew how to code an AJAX application

Other Important Information

1. My example uses responseText to display the server's output. Alternatively, you can use XML (responseXML) as output and have your client parse the results and display appropriately.

2. My example did not send any parameters to the server. This can be easily done by appending the URL before sending the request to the server and have the processing script grabbing the parameters via GET. You can also use POST to send your parameters to the server. Refer to the following resources for more info.

Other Useful Resources
The following are some of the top sites that contains very useful information about AJAX:

1. http://ajaxpatterns.org/Main_Page - The main source of AJAX knowledge!

2. http://ajaxian.com/ -Useful information for reference.

3. http://ajaxblog.com/ -Another great site for AJAX examples.

4. http://www.ajaxload.info/ -Contains tons of useful loading images for free use.

Enjoy this AJAX


Please register or log-in into NamePros to hide ads

Last edited by AbsoluteKC : 10-23-2006 at 01:42 AM. Reason: Correct Typo, see red.
AbsoluteKC is offline   Reply With Quote
Old 10-22-2006, 07:28 PM   · #2
BillyConnite
 
BillyConnite's Avatar
 
Name: Rhett
Location: Coffs H, Australia
Trader Rating: (77)
Join Date: Jul 2005
Posts: 3,106
NP$: 47.00 (Donate)
BillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant future
Wildlife Parkinson's Disease Parkinson's Disease
Nice tut! Thanks AbsoluteKC.

I've been looking for a basic tut on AJAX, haven't looked at AJAX much yet.

Rep added, thanks again.
__________________
<?php if(1===1){ $computer="fine."; }else{ $computer="broken."; } echo "Your computer is ".$computer; ?>
BillyConnite is offline   Reply With Quote
Old 10-22-2006, 10:18 PM   · #3
-Nick-
I'll do it
 
-Nick-'s Avatar
 
Name: Keral. Patel.
Location: India
Trader Rating: (97)
Join Date: Dec 2005
Posts: 5,353
NP$: 12078.05 (Donate)
-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute
Member of the Month
September 2007 Adoption
Now this is called digested content.

I liked it a lot. Will play around with this and hoping will create some nice web applications if my brains support me.

Thanks. Rep Added.
-Nick- is offline   Reply With Quote
Old 10-23-2006, 04:06 AM   · #4
kleszcz
 
kleszcz's Avatar
 
Trader Rating: (240)
Join Date: Jul 2006
Posts: 3,405
NP$: 539.20 (Donate)
kleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond repute
Marrow Donor Program Multiple Sclerosis
Great resources! Thanks AbsoluteKC. Rep added.
__________________
Certifed.us - Premiun .us Names
KQDO.com - $1 start
HUVW.com - $1 start
kleszcz is offline   Reply With Quote
Old 10-23-2006, 06:05 AM   · #5
-Ray-
An American Soldier
 
-Ray-'s Avatar
 
Name: Ray
Location: Pennsylvania
Trader Rating: (25)
Join Date: Jun 2005
Posts: 1,522
NP$: 347.25 (Donate)
-Ray- is a splendid one to behold-Ray- is a splendid one to behold-Ray- is a splendid one to behold-Ray- is a splendid one to behold-Ray- is a splendid one to behold-Ray- is a splendid one to behold-Ray- is a splendid one to behold
Does that differencate browsers... firefox or ie.. because i believe firefox uses a different techniue for the refresh
-Ray- is offline   Reply With Quote
Old 10-23-2006, 07:07 AM   · #6
AbsoluteKC
NamePros Regular
 
AbsoluteKC's Avatar
 
Trader Rating: (12)
Join Date: Apr 2006
Posts: 691
NP$: 60.55 (Donate)
AbsoluteKC is a splendid one to beholdAbsoluteKC is a splendid one to beholdAbsoluteKC is a splendid one to beholdAbsoluteKC is a splendid one to beholdAbsoluteKC is a splendid one to beholdAbsoluteKC is a splendid one to behold
Originally Posted by templaterave
Does that differencate browsers... firefox or ie.. because i believe firefox uses a different techniue for the refresh


The example is compatible in all browsers that support:

1. XmlHttpRequest object
2. JavaScript

The main concept behind AJAX is the XHR object. Therefore, it works the same in IE and FF etc.
AbsoluteKC is offline   Reply With Quote
Old 01-16-2007, 07:42 AM   · #7
sathish5566
New Member
 
Trader Rating: (0)
Join Date: Jan 2007
Posts: 5
NP$: 0.00 (Donate)
sathish5566 is an unknown quantity at this point
nice thank you rep
sathish5566 is offline   Reply With Quote
Old 02-15-2007, 02:05 PM   · #8
YesBrilliant
Account Closed
 
Trader Rating: (21)
Join Date: Sep 2006
Posts: 1,075
NP$: 0.00 (Donate)
YesBrilliant is a name known to allYesBrilliant is a name known to allYesBrilliant is a name known to allYesBrilliant is a name known to allYesBrilliant is a name known to allYesBrilliant is a name known to all
Originally Posted by -Nick-
Now this is called digested content.


Tottally agree!

Very interesting as well!

Thanks for sharing!
YesBrilliant is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Site Sponsors
Buy Flash Arcade Game Script Get Your Site Linked at LinkedKeywords.com Find out how!
Advertise your business at NamePros
All times are GMT -7. The time now is 08:11 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0