Home

See the related posts

PHP Warning: Call-time pass-by-reference has been deprecated    Working with Database (MySQL)    Cookies in PHP    Session Management Functions in php    Join array elements with a string – implode   

Ajax examples with php

Ajax is a catchy name for a type of programming made popular in 2005 by Google and other big web developers. Ajax loosely stands for Asynchronous Javascript And XML, but that just sounds like techno jargon to many people.

<!--
AJAX CODE BY samplephpcodes.com	

makeGetRequest('file.php?uid=<?php echo $fid; ?>',result_div_id)

-->
<!-- ++++++++++++++++++++++++++++++++++++++++ -->

<script language="javascript">

function createRequestObject() {

    var tmpXmlHttpObject;

    if (window.XMLHttpRequest) { 

        // Mozilla, Safari would use this method ...

        tmpXmlHttpObject = new XMLHttpRequest();

    } else if (window.ActiveXObject) { 

        // IE would use this method ...

        tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");

    }

    return tmpXmlHttpObject;

}

var http = createRequestObject();

var out;

function makeGetRequest(url,temp) {

   	out=temp;
	var val=Math.random();

	url=url+'&sid='+val;
	http.open('get', url);

	http.onreadystatechange = processResponse;

	http.send(null);

}

function processResponse() {

     if(http.readyState == 4){

        var response = http.responseText;

	    document.getElementById(out).innerHTML = response;

       }

}

</script>

<!-- +++++++++++++++++++++++++++++++++++++++++++++++ -->

After including the following code in the head part of the html page call the followin javascript function wereru need to call you ajax request


makeGetRequest('file.php?uid=<?php echo $fid; ?>',result_div_id)

In the above Javascript code, we try three times to make our XMLHttpRequest object. Our first attempt:

* ajaxRequest = new XMLHttpRequest();

is for the Opera 8.0+, Firefox and Safari browsers. If that fails we try two more times to make the correct object for an Internet Explorer browser with:

* ajaxRequest = new ActiveXObject(“Msxml2.XMLHTTP”);
* ajaxRequest = new ActiveXObject(“Microsoft.XMLHTTP”);>

If that doesn’t work, then they are using a very outdated browser that doesn’t support XMLHttpRequest, which also means it doesn’t support Ajax.

Most likely though, our variable ajaxRequest will now be set to whatever XMLHttpRequest standard the browser uses and we can start sending data to the server.

Home

See the related posts

PHP Warning: Call-time pass-by-reference has been deprecated    Working with Database (MySQL)    Cookies in PHP    Session Management Functions in php    Join array elements with a string – implode   

1 Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment