Showing posts with label Simple AJAX - PHP and Javascript. Show all posts
Showing posts with label Simple AJAX - PHP and Javascript. Show all posts

javascript ajax post

function Inint_AJAX() 
{

   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert("XMLHttpRequest not supported");
   return null;

}

var http = Inint_AJAX(); var url = "example.php"; var params = "content=1"; http.open("POST",url+"?"+params, true); http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length", params.length); http.setRequestHeader("Connection", "close"); http.onreadystatechange = function() {//Call a function when the state changes. if(http.readyState == 4 && http.status == 200) { document.getElementById('divid').innerHTML=http.responseText; //retuen value }

Simple AJAX - PHP and Javascript

this tutorial I will show you the most basic way to get a response from a PHP script.and we will start with the base object you need: a XMLHTTP Javascript object. With this object you call a server-side script and get the response back. Most browsers use the same Javascript object, but of course Microsoft has to be different, so they require their own. The following function will return the correct object:
function Inint_AJAX() {

   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE

   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE

   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript

   alert("XMLHttpRequest not supported");

   return null;

};
This code will, again, return an XMLHTTP object to use in our Javascript. What is going to happen is that we are going to use this object to make an HTTP request through Javascript. When we get a response back, we will simply place it in the page, nothing fancy. In order to make the request we use the following code:
function ajaxfun(src)

{

     var req = Inint_AJAX();

     req.onreadystatechange = function () { 

          if (req.readyState==4 && req.status==200) {

                  document.getElementById('divid').innerHTML=req.responseText; //retuen value
          }

     };

     req.open("GET", "demo.php?src="+src); //make connection

     req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header

     req.send(null); //send value

   

}