Welcome Guest ( Log In | Register )



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Ajax, For Dummies!
Habble
post Nov 19 2007, 05:35 AM
Post #1


Premium Member
Group Icon

Group: [HOSTED]
Posts: 255
Joined: 17-June 07
From: Tasmania
Member No.: 22,699



Most of you would have heard of AJAX. What is it? Is it a language? No. It's JavaScript.

Basically, AJAX lets us get the content from an external page without reloading! It's especially useful when you need to get information from a serverside script (e.g. A php page that gets information from a database), and you don't want the page reloading all the time.

So how do you use AJAX? Let's make an example.
Make a new html document. Let's call it ajaxexample.htm.

Make it look something like this:
CODE
<html>
<head>
<title>AJAX Example</title>
</head>
<body>
<a href="#" onClick="doAjax();">Click here!</a><br><br>
<span id="ajaxspan"></span>
</body>
</html>

Note this line:
<a href="#" onClick="doAjax();">Click here!</a>
We make the href parameter "#", because just plain "" would cause the page to reload when we clicked the link, and not putting the href parameter at all would mean the link wouldn't be coloured/underlined. This doesn't matter all that much, but it makes a nicer effect that just clicking on text.
This line:
<span id="ajaxspan"></span>
Will be used when we actually make the script, which we're doing now!

Place this in the head tags of your page:
CODE
<script language="JavaScript" type="Text/JavaScript">
function GetXmlHttpObject()
{
    var objXMLHttp = null;
    if (window.XMLHttpRequest)
    {
        objXMLHttp = new XMLHttpRequest(); //Checks if the XMLHttpRequest variable should be used
    }
    else if (window.ActiveXObject)
    {
        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //Checks if Microsoft.XMLHTTP should be used (For IE)
    }
    return objXMLHttp;
}

function doAjax()
{
    xmlHttp = GetXmlHttpObject(); //Set the xmlHttp variable
    url = 'ajaxpage.php'; //Set the url variable to ajaxpage.php. We'll make this page later
    xmlHttp.open('GET', url, true); //Tell the client what page we want, and how to get it
    xmlHttp.onreadystatechange = updateSpan; //Tell the client what function to do when the state of xmlHttp changes
    xmlHttp.send(null); //Sends the request
    document.getElementById('ajaxspan').innerHTML = 'Loading...'; //Sets the text inside the "ajaxspan" element to "Loading...", so the user knows it's doing something!
}

function updateSpan()
{
    if (xmlHttp.readyState==4) //If xmlHttp's state is 4 (the page is loaded)
    {
        document.getElementById('ajaxspan').innerHTML = xmlHttp.responseText; //sets the "ajaxspan" element to whatever was on the page
    }
}
</script>

Big bit of code, isn't it!

Basically, it sets the xmlHttp variable to whatever's the best Ajax XmlHttp variable for the browser, then it tells the client to load the page "ajaxpage.php", and puts "Loading..." onto the page while it loads, then when it loads, it replaces the loading text with whatever's on the page!

Now for the final bit, ajaxpage.php. You can do this however you want, here's a simple example that just shows "Hello world!"
CODE
<?php
echo 'Hello, world!';
?>

Simple, huh?

Your final ajaxexample.htm page should look like this:
CODE
<html>
<head>
<title>AJAX Example</title>
<script language="JavaScript" type="Text/JavaScript">
function GetXmlHttpObject()
{
    var objXMLHttp = null;
    if (window.XMLHttpRequest)
    {
        objXMLHttp = new XMLHttpRequest(); //Checks if the XMLHttpRequest variable should be used
    }
    else if (window.ActiveXObject)
    {
        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //Checks if Microsoft.XMLHTTP should be used (For IE)
    }
    return objXMLHttp;
}

function doAjax()
{
    xmlHttp = GetXmlHttpObject(); //Set the xmlHttp variable
    url = 'ajaxpage.php'; //Set the url variable to ajaxpage.php. We'll make this page later
    xmlHttp.open('GET', url, true); //Tell the client what page we want, and how to get it
    xmlHttp.onreadystatechange = updateSpan; //Tell the client what function to do when the state of xmlHttp changes
    xmlHttp.send(null); //Sends the request
    document.getElementById('ajaxspan').innerHTML = 'Loading...'; //Sets the text inside the "ajaxspan" element to "Loading...", so the user knows it's doing something!
}

function updateSpan()
{
    if (xmlHttp.readyState==4) //If xmlHttp's state is 4 (the page is loaded)
    {
        document.getElementById('ajaxspan').innerHTML = xmlHttp.responseText; //sets the "ajaxspan" element to whatever was on the page
    }
}
</script>
</head>
<body>
<a href="#" onClick="doAjax();">Click here!</a><br><br>
<span id="ajaxspan"></span>
</body>
</html>


And if it all goes well, when you click the "Click here!" link, the text "loading..." should appear, then once it's loaded, "Hello world!"

It should be easy to fiddle around and modify it to suit your needs. I hope you learned a little about AJAX!

This post has been edited by Habble: Nov 22 2007, 05:53 AM
Go to the top of the page
 
+Quote Post
toby
post Nov 19 2007, 05:23 PM
Post #2


Premium Member
Group Icon

Group: Members
Posts: 422
Joined: 29-September 06
Member No.: 16,228



http://craig.veritynet.net/~roly/

Doesn't work for me. Really good tut though, I do understand it now.
Go to the top of the page
 
+Quote Post
Jimmy89
post Nov 20 2007, 12:50 AM
Post #3


Living at the Datacenter
Group Icon

Group: [HOSTED]
Posts: 696
Joined: 30-June 06
From: Australia
Member No.: 14,219



Thats a nice introduction tutorial. Thanks, got anything else for us anytime soon? tongue.gif
Go to the top of the page
 
+Quote Post
kelvinmaki
post Nov 21 2007, 02:55 AM
Post #4


Advanced Member
Group Icon

Group: Members
Posts: 170
Joined: 30-July 07
Member No.: 23,704



Nice tutorial. There's also some javascript framework that you don't have to write much code to implement AJAX. If you are interested,

1. Prototype
2. JQuery
3. Mootools

Cheers
Go to the top of the page
 
+Quote Post
Habble
post Nov 21 2007, 05:53 AM
Post #5


Premium Member
Group Icon

Group: [HOSTED]
Posts: 255
Joined: 17-June 07
From: Tasmania
Member No.: 22,699



QUOTE(toby @ Nov 19 2007, 05:23 PM) *
http://craig.veritynet.net/~roly/

Doesn't work for me. Really good tut though, I do understand it now.


Oops, my bad! JavaScript's case sensitive, it should be responseText, not responsetext

I'll change it now!
Go to the top of the page
 
+Quote Post
toby
post Nov 21 2007, 01:07 PM
Post #6


Premium Member
Group Icon

Group: Members
Posts: 422
Joined: 29-September 06
Member No.: 16,228



Thanks.

What changes? Is it the ajaxspan, the loading and the xmlhttpopen? What different parameters can be in that last one?
Go to the top of the page
 
+Quote Post
rraallvv
post Jan 8 2008, 03:31 PM
Post #7


Advanced Member
Group Icon

Group: Banned
Posts: 119
Joined: 2-January 08
Member No.: 27,304



Great job...

The next step could be the tutorial in this post http://www.astahost.com/index.php?showtopi...502&hl=ajax

Regards.
Go to the top of the page
 
+Quote Post
kelvinmaki
post Jan 9 2008, 02:52 AM
Post #8


Advanced Member
Group Icon

Group: Members
Posts: 170
Joined: 30-July 07
Member No.: 23,704



QUOTE(rraallvv @ Jan 8 2008, 03:31 PM) *
Great job...

The next step could be the tutorial in this post http://www.astahost.com/index.php?showtopi...502&hl=ajax

Regards.


That's another nice tutorial for those interested in Ajax. If for those who wants to get a feel and some background of it, I found this particular article that cover the whole of Ajax in an overview. Read it in your free time as its 9 pages . tongue.gif

http://www.peachpit.com/articles/article.a...425820&rl=1
Go to the top of the page
 
+Quote Post
Jeigh
post Jan 27 2008, 03:30 PM
Post #9


Whitest Black Mage
Group Icon

Group: [MODERATOR]
Posts: 1,301
Joined: 20-May 05
From: NB, Canada
Member No.: 5,281



Ajax is one of the few web based tech's I haven't had the chance to try my hand at yet, but luckily I'm in a course now that will be hitting on ajax a bit near the end so that should be a nice treat. I know it seems like a fairly powerful step forward for dynamic webpage generation so I'm excited to see what its capable of once I dig into it a bit. I always enjoy the new technologies they put out on web platforms so this should be entertaining as well.
Go to the top of the page
 
+Quote Post
java-area
post Feb 4 2008, 05:06 PM
Post #10


Member [ Level 1 ]
Group Icon

Group: [HOSTED]
Posts: 47
Joined: 4-February 08
Member No.: 28,119



AJAX (like javascript) is browser-dependable.
That's way XMLHttpRequest object can be created in different browsers using different javascript methods.
Open-source library Larissa hides this dependancy and provides some cross-browsers methods.
Go to the top of the page
 
+Quote Post

2 Pages V   1 2 >
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Ajax + Php + Sql = Simply Superb! ( With Visitor Tracking )(10)


 



- Lo-Fi Version Time is now: 7th July 2008 - 02:50 AM