AJAX -- Hmm.. I'll show my n00biness
Posted: Thu Feb 02, 2006 3:58 pm
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?? 
/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:
Server side:
That's it... that's the nitty gritty of it. I feel like I've been left behind all this time 
//Pardon the excitement
/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>Code: Select all
<?php
echo "Yes... It works!";
?>//Pardon the excitement