Tuesday, 11 June 2013

How to use Ajax in php to get data from server?

Ajax is a technology that helps us load the data from server without loading the whole page.
We can make ajax calls that send requests to server and display the result on the fly without loading the page.

Below java script code will demonstrate how we can use ajax in php.

<script>
function getajaxdata(param)
{

  xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
         alert ( xmlhttp.responseText );
      }
  }

 xmlhttp.open("GET","ajaxserverpage.php?p="+param,true);
 xmlhttp.send();
}
</script>

As shown above We have made an ajax call using below line

 xmlhttp.open("GET","ajaxserverpage.php?p="+param,true);

Here what we are trying to do is calling the script in ajaxserverpage.php page .

When the server sends the response we are showing it to user. Normally you will display the response in some section on your page.









No comments:

Post a Comment