Cesar D. Rodas, web development. Technology news. PHP, MySQL, Apache, C, Bash, ASM

PHP: implementing a heavy use of AJAX

Today, the trend is to implement AJAX in everything related with Web. AJAX (Asynchronous JavaScript and XML) is not a new programming language, It is a “style” of development that mix two technologies, the client side (JavaScript) and the server site (usually PHP, JSP, Java or other).

The client side could request just a part of the page instead of download the hole page. Until this point everything is good, but not always is so good, such benefits needs more development, and AJAX is not well standarized.

Let’s start with AJAX:

Basic Example

Html:

<!– Clicking this link initiates the asyncronous request –>
<a href=“javascript:void(0)” onclick=supportAjax && sayHello();”>Say Hello</a><br />
<!– used to display the results of the asyncronous request –>
<span id=“span_result”></span>

Javascript:

var supportAjax;
function createAjaxObj() {
       supportAjax=true;
       if (window.XMLHttpRequest) {
           return new XMLHttpRequest(); //Not IE
       } else if(window.ActiveXObject) {
           return new ActiveXObject(“Microsoft.XMLHTTP”); //IE
       } else {
           supportAjax=true;
       }
}

var receiveReq = createAjaxObj();
function sayHello() {
	//If our XmlHttpRequest object is not in the middle
        //of a request,
        //start the new asyncronous call.
	if (receiveReq.readyState == 4 ||
                   receiveReq.readyState == 0) {
		//Setup the connection as a GET call to
                //SayHello.php.
		//True explicity sets the request
                //to asyncronous (default).
		receiveReq.open(“GET”, ‘SayHello.php’, true);
		//Set the function that will be called when the
                //XmlHttpRequest objects state changes.
		receiveReq.onreadystatechange = handleSayHello;
		//Make the actual request.
		receiveReq.send(null);
	}
}
//Called every time our XmlHttpRequest objects state changes.
function handleSayHello() {
	//Check to see if the XmlHttpRequests state is finished.
	if (receiveReq.readyState == 4) {
		//Set the contents of our span element to
                // the result of the asyncronous call.
		elem = document.getElementById(’span_result’);
                elem.innerHTML = receiveReq.responseText;
	}
}

As you can see write AJAX pages is not so short, Image that you have to do a Web Portal such as Yahoo Mail, Gmail, or something else that require a heavy use of AJAX.

I usually work with Javascript class that made easy develop AJAX apps.

Download AJAX Javascript

And Here is an example how to implement this JS class:

<script  language=JavaScript1.1 src=ajax.js></script>
<script language=JavaScript1.1>
    function ajax_process_ok(obj)
    {
	//do something, the text
	// is in “obj.responseText”
    }

    function ajax_process_err(obj)
    {
        //do something
    }

    /*
     *	This is the function that has the ajax function
     */
    function AjaxQuery()
    {
        //sucess function handler
        var success  = function(t){ajax_process_ok(t);}
        //error function handler
        var failure  = function(t){ajax_process_err(t);}
        var url = ./hellopage.php;
        var myAjax = new Ajax.Request(url,
            {
                method:get,  onSuccess:success,
                onFailure:failure
            }
        )
    }

</script>

BTW, there exist others simplest method to write AJAX Apps, but I dont know to use it, I reccoment to visit PHP: Writing XAJAX and Smarty Templates

Also I’ve listening many goods things about scriptaculous, prototype, and don´t forgot to check these too

Also I recomment to see the PHP AJAX Section

The entrepreneurs of the modern age are enhancing their business scope in the global world via online marketing tactics. The best option of promoting your business products is email marketing by which you may approach the potential clients. The proper pay per click management services of search engine optimizers are offered in the different rates to the clients. The best strategies of internet advertising are applied by professional advertisers to run advertising campaign successfully.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. Digg del.icio.us StumbleUpon Technorati BlinkList Furl NewsVine Reddit

8 Responses to “PHP: implementing a heavy use of AJAX”

  1. programame.net Says:

    php y AJAX…

    Como comenzar con AJAX y PHP, varios ejemplos……

  2. www.webeame.net Says:

    php y AJAX…

    Como comenzar con AJAX y PHP, varios ejemplos……

  3. php y AJAX - Noticias externas Says:

    […] Como comenzar con AJAX y PHP, varios ejemplos…» noticia original […]

  4. admin Says:

    This a test for proof the new captcha!

  5. Cheap Web Hosting Says:

    I have page with refreshing frames. It refreshes every 1 minute. I want to make this refresh through AJAX. How can I proceed?

  6. admin Says:

    Give me the URL of your site and I will help you

  7. Rochak Chauhan Says:

    To know more about AJAX and its usage, you can visit my blog at:
    http://rochakchauhan.com/blog/category/programming/ajax/

  8. felipe Says:

    Si César habla español, sería mejor que escribiera en español, antes que inglés, pues hay mucha documentación en inglés, mas los hispanos merecen acceder a la información más fácil para aprender mejor y superar el subdesarrollo en comunidad.

Leave a Reply


Fatal error: Call to undefined function display_cryptographp() in /home/.schmuckie/saddor/cesarodas.com/wp-content/themes/first-spring-10/comments.php on line 94