AJAX is the way of communicating with a back end server without having to send the full information (you can of course) but for example you could just send a username check to see if it is available, but the main thing is that you do not send back a full page but only the part that you want to update.
With reference to the example of a username, you could just send the username and send back either yes or no response which saves allot of time and traffic from the client to the server (and makes the whole web page experience nicer).
All the AJAX is shorthand for “Asynchronous JAvascript and Xml”, asynchronous means that you can do something else whilst waiting for the response (put the kettle on and get a cup for the drink whilst the kettle is boiling) thus with javascript on the client web browser sends a request to a web page on the server with XML wrappings.
To get the basics lets start with the being
// this function will return a XmlHttpRequest object that allows you to "talk" to the server. function GetXmlHttpObject() { // IE7+, FF, Chrome, Opera, Safari if (window.XMLHttpRequest) return new XMLHttpRequest; // IE6 , IE5 if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); } |
the GetXmlHttpObject will return a object that will allow the javascript to talk to the backend server, the newer version is call a XMLHttpRequest whilst on older browsers it was part of the ActiveXObjects.
The next is to send a request to the backend server
xmlHttpObject.onreadystatechange=callBackFunction; xmlHttpObject.open("GET", GetUrl, true); xmlHttpObject.send(null); |
The onreadystatechange, will call a function (in javascript on the client browser) when the request alters from different states, the different states are
- 1.open method invoked successful, open a connection with the server
- 2.server responsed with a valid header response.
- 3.server has sent some data, the response content is started to load.
- 4.server has finished sending all of data
so from reading the states, you are really interested in state 4, because that will have the data (server response) that you are interested in for this.
The .open forms the request to the XmlHttpRequest object to call (“GET” in HTML) the server web site, the GetUrl is just a variable that well call a php page (“ajaxbackendcall.php”) which takes a parameter called name and returns a string with the name in reverse (shall include that source code later).
And then the .send will start the ready states to change and sends the request to the backend server, here is the function that is called on the state change
function callBackFunction() { if (xmlHttpObject.readyState == 4) alert(xmlHttpObject.responseText); } |
What is happening here, is that from the state stages I am waiting on ready state to equal 4 ( when the server has finished responding) and then output the response from the responseText which is filled from the AJAX call to the backend server.
That is mainly it, here is some full code for you to try out save this as “codingfriends.com.ajax.test.html”
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html3/strict.dtd"> <html> <head> <script type="text/javascript"> // the xmlHttpRequest object var xmlHttpObject; // the ajax call function getInnerText() { // get the name to send to the server backend page var namehere = document.getElementById("namehere"); // request the xmlHttpObject xmlHttpObject = GetXmlHttpObject(); // if there is not xmlHttpRequest object being allowed to be created then the browser does not support it. if (xmlHttpObject==null) { alert("Browser does not support XmlHttp calls e.g. AJAX"); return; } // fill in the request details, e.g. the url to call and also the query string inserted into the url var GetUrl = "ajaxbackendcall.php"+"?name="+namehere.value; // here is the call to the server. // to start with setup which function to call when a ready state on the XmlHttpRequest object has changed xmlHttpObject.onreadystatechange=callBackFunction; // request the html "GET" to obtain the url (e.g. the backend server page, dynmaic normally) xmlHttpObject.open("GET", GetUrl, true); // and then issue it xmlHttpObject.send(null); } // the function that is called once the XmlHttpRequest object state has changed /* the readyStates are 1 open method invoked successful, open a connection with the server 2 server responsed with a valid header response. 3 server has sent some data, the response content is started to load. 4 server has finished sending all of data */ // so we listen for readyState 4 when all finished // and output the response text into the div id innertextoutput function callBackFunction() { if (xmlHttpObject.readyState == 4) document.getElementById("innertextoutput").innerHTML=xmlHttpObject.responseText; } // this function will return a XmlHttpRequest object that allows you to "talk" to the server. function GetXmlHttpObject() { // IE7+, FF, Chrome, Opera, Safari if (window.XMLHttpRequest) return new XMLHttpRequest; // IE6 , IE5 if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); } </script> </head> <body> <a href="#" onclick="javascript:getInnerText()">click here to return text from a ajax call</a> <br/> Enter the name here <input id="namehere"/> <br/> <div id="innertextoutput"></div> </body> </html> |
and then save this as the ajaxbackendcall.php file to call (in the same directory as the code above and also within a directory that has PHP plugin enable for that webserver be it apache or IIS.
<?php $name = $_GET['name']; // change the output to say something else, here I am just reversing the name if (strlen($name) > 0) { // have to insert something into it so that php does not make it into a array but a string $namereturn="0"; $j = 0; for ($i = strlen($name)-1; $i >= 0; $i--) { $namereturn[$j++]=$name[$i]; } echo "normal name {$name} and in reverse {$namereturn}"; } else return "No name inputted"; ?> |
The output would be similar to the below. OUTPUT
click here to return text from a ajax call
Enter the name here
END OF OUTPUT
If you do not have a PHP webserver to test with, you can just alter the codingfriends.com.ajax.test.html code by altering the backend web page to call to this
var GetUrl = "nonephpbackend.html" |
and create a page within the same directory as the codingfriends.com.ajax.test.html page and place something inside it like
hi there |