Wednesday, October 10, 2012

Php Request Handlers Ajax

This is similar to how facebook uses ajax. It is the closest i have got.
This is the ajax script. place it in head section or create a js. page for it and include it. index.php.

<html>
<head>
<script language="javascript"  type="text/javascript">
function doHttpRequest() {  // This function does the AJAX request
  http.open("GET", "http://127.0.0.1/ajaxer/formdata.php", true);
  http.onreadystatechange = getHttpRes;
  http.send(null);
}
function doHttpRequest2() {  // This function does the AJAX request
http.open("GET", "http://127.0.0.1/ajaxer/formrequest.php?txt1="+document.getElementById('txt1').value, true);
  http.onreadystatechange = getHttpRes;
  http.send(null);
}
function getHttpRes() {
  if (http.readyState == 4) {
        res = http.responseText;  // These following lines get the response and update the page
        document.getElementById('div1').innerHTML = res;
  }
}
function getXHTTP() {
  var xhttp;
   try {   // The following "try" blocks get the XMLHTTP object for various browsers…
          xhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
                xhttp = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e2) {
          // This block handles Mozilla/Firefox browsers...
          try {
                xhttp = new XMLHttpRequest();
          } catch (e3) {
                xhttp = false;
          }
          }
        }
  return xhttp; // Return the XMLHTTP object
}
var http = getXHTTP(); // This executes when the page first loads.
</script>
</head>
<body align="center">
<input align="center" type="button" value="Press for AJAX" name="btn" onClick="doHttpRequest();" >
<br><br>
<div align="center" id="div1"> <h3> Dynamic AJAX will be displayed here. </h3>  </div>   <!---this will be the dynamic div that does the majic. //--->
</body>
</html>

This is the form show method Page. formdata.php.



<h3 align="center">Here is an HTML Form, enter data, click submit…</h3>
<Form align="center" name="myform">
          Type text: <input align="center" type="text" size="15" id="txt1" >
  <br><br>
          <input align="center" type="button" value="Press to submit form" onClick="doHttpRequest2();">
</Form>

And this is the either recieve data request page or send data action page without page refresh. formrequest.php.



<HTML>
<body>
<h3 align="center">Here is the Form field text: <?php echo $_GET["txt1"]; ?></h3><br />
<p>Welcome to the most amazing site !</p>
</body>
</HTML>
Facebook uses everything but Php runs all the functions, classes, views, and validation and database scripts. Ajax Push and Pulls most of the Php page request, and handles all their Url Request, and Jquery handles the chat scrollers and C++ is used for parsing xhtml from outside sources, for your video-chat downloaded chat and, and lastly for the apps and game integration and application sharing API'S for all sites outside sourcing. .
You will want to sanitize the Txt1 Data when it gets retrieved by your action page !
Play with this for a while. I have already created a wall posting script identical to facebooks that im using in a business portal using this altered with the same req methods.. Enjoy. 

1 comment: