Monday, June 3, 2013

WHAT IS AJAX

IT STANDS FOR ASYNCHRONOUS JAVASCRIPT ASYNCHRONOUS XML .AJAX is widely used for real time results from server like data needed for a site showing sharebazzar news or one showing cricket scores.

It is quite easy to learn AJAX

Just simply crate a AJAX Object and call it through JAVASCRIPT

Here is a simple function to create xmlhttp object
function the_Object() {
   
    var xmlhttp;
    // This if condition for Firefox and Opera Browsers
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try  {
            xmlhttp = new XMLHttpRequest();
        }
        catch (e) {
            alert("Your browser is not supporting XMLHTTPRequest");
            xmlhttp = false;
        }
    }
    // else condition for ie
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlhttp;
}
var request = the_Object();

THIS OBJECT THEN USED WITH  JAVASCRIPT TO PERFORM TASK

HERE IS EXAMPLE

function searchdata(){

var tosearch = document.getElementById("txtsearch").value;
var searchby = document.getElementById("selfld").value;
if(searchby=='0'){
     alert("Please select Category");
return false;
}
var url = "search.php";// PHP SCRIPT FILE
url1= url+"?tosearch="+tosearch+"&searchby="+searchby; //PASSING VARIABLE
request.open("GET", url1, true);//OPEN URL

request.onreadystatechange = function aa() {//Call a function when the state changes.
       
        if(request.readyState == 4 && request.status == 200) {
            ans=request.responseText;
            document.getElementById("test").style.visibility = 'visible';
            document.getElementById("test").innerHTML=ans;//PLACE OUTPUT
            }
}
request.send(null);
}

THIS IS HOW AJAX WORKS

No comments:

Post a Comment