Page 1 of 1

AJAX -- Hmm.. I'll show my n00biness

Posted: Thu Feb 02, 2006 3:58 pm
by Chris Corbyn
Oh my god! I finally decided to learn how to write AJAX without the need for a library like XAJAX. I was think it would be one of those things that takes a while to grasp. It's easy and took me 10 mins to suss... why have I been relying on 3rd party stuff all this time?? :oops:

/me kicks self

Granted I've only covered the basic idea but the rest is history once you have that down.

Simplest bit of AJAX possible.

Client side:

Code: Select all

<script type="text/javascript">
<!--
//For cross browser compatibility you'd do some checking but that's nothing to do with AJAX itself
var http = new XMLHttpRequest();

function showText()
{
    //Send the GET request
    http.open("GET", 'server_script.php', true);
    //When response is received, pass to this callback function
    http.onreadystatechange = handleResponse;
    //Send the request
    http.send(false);
}

function handleResponse()
{
    if (http.readyState == 4) //If successful
        document.getElementById('foo').innerHTML = http.responseText;
}
// -->
</script>
<a href="javascript:showText();">Click here to get AJAX data</a>
<div id="foo">Text will show here</div>
Server side:

Code: Select all

<?php

echo "Yes... It works!";

?>
That's it... that's the nitty gritty of it. I feel like I've been left behind all this time :lol:

//Pardon the excitement

Posted: Thu Feb 02, 2006 4:15 pm
by foobar
I still prefer XAJAX despite having programmed my own AJAX functions.
It's got lots of functionality out of the box. Couple that with my ineptness at JavaScript... you get the picture.+

Congrats on taking that bold step forward, though! ;)

Posted: Thu Feb 02, 2006 4:29 pm
by Chris Corbyn
foobar wrote:I still prefer XAJAX despite having programmed my own AJAX functions.
It's got lots of functionality out of the box. Couple that with my ineptness at JavaScript... you get the picture.+

Congrats on taking that bold step forward, though! ;)
XAJAX is nice but very heavy. I guess it depends how much you need to do. I'd still reccommed xajax to anyone wanting to just try ajax out without worrying how it works. Thing is... it took me *longer* to learn how to do what the above script does using XAJAX than what it took to do it using ajax by itself 8O