• 18th May 2007 - By Vinu Thomas

    If you’re looking for the quickest way to get your PHP functionality Ajax enabled, you’ve got to take a look at PHPLiveX. It greatly simplifies the process of Ajaxifying your PHP code without knowing Javascript code. It’s worth learning this technique, as whether you’ve got a deadline or you’re just sorting out the length of other methods in between your server set-up and your O2 Broadband being installed, it’s a great way to keep productive and efficient.

    Let’s see how to Ajaxify a simple PHP function. Let’s create a simple function to output the server time:

    function myServerTime(){
    	return date("D M j G:i:s T Y");
    }

    First you have to include the PHPLiveX library and register your function. Here’s how to to it:

    < ? php
      include 'PHPLiveX.php';
     
    	function myServerTime(){
    		return date("D M j G:i:s T Y");
    	}
     
      $phplive = new PHPLiveX("myServerTime"); //Register the function with PHPLiveX
    ?>

    $phplive = new PHPLiveX(“myServerTime”); is used to register our function myServerTime with PHPLiveX

    Next let’s build our HTML Display. Let’s keep it simple, but allowing the user to click a link to get the updated server time:

     <a>Get Server Time </a>

    Now we’ll have to create areas to display our Loading Status and the results back from the server. Let’s create two divs for this.

    <span id="loading" style="visibility: hidden;">Loading...</span>

    You’ll notice that the loading span’s visibility has been set to hidden by default. PHPLiveX will use this to show the loading indicator when an action is being performed.

    Now let’s get the entire HTML created. PHPLiveX autogenerates the Javascript required to create the AJAX calls, this has to take place in the html head section. To generate the Javascript all you need to call is $phplive->Run(). So let’s get to the entire script with the HTML:

    < ? php
      include 'PHPLiveX.php';
     
    	function myServerTime(){
    		return date("D M j G:i:s T Y");
    	}
     
      $phplive = new PHPLiveX("myServerTime"); //Register the function with PHPLiveX
    ? >
     
    <a href="javascript:myServerTime('', 'target=serverResult,preload=loading');">Get Server Time</a>
     
    <span id="loading" style="visibility: hidden;">Loading...</span>

    You’ll notice that in the Get Server Time link there’s a little bit of Javascript – javascript:myServerTime(”, ‘target=serverResult,preload=loading’);. That’s used to call the myServerTime function from Javascript. The first parameter is null since our function myServerTime doesn’t accept any parameters. The second parameter tells PHPLiveX to send the output to the “serverResult” div and use the “loading” div as the preloader.

    That’s it ! You can see a demo of this here.
    Get the source code for this tutorial here. This includes version 2.2 of the PHPLiveX library.

    I’ll put up a tutorial on how to use PHPLiveX for a whois lookup next week.

    Link to PHPLiveX: PHPLiveX -> An Open Source PHP/Ajax Library

  • 9 Comments to “PHPLiveX – Tutorial”

    • [...] Thomas points out an interesting bit of technology in his latest post today – PHPLiveX, a bit of Ajax to PHP “glue” that doesn’t require you to know [...]

    • Rehan on May 22, 2007

      Nice tutorial, but shouldn’t the script above $plx->Run(); be $phplive->Run(); ??

    • vinu on May 22, 2007

      Thanks for pointing out my blunder in the code, Rehan. I’ve made the required changes to the code in the tutorial above.

    • Gurpreet on June 19, 2007

      Hi,
      It seems that this is not working in IE6. The Javascript error says “Object Expected”. Any ideas??
      If it works in IE, it could be a blessing for me :(

    • vinu on June 20, 2007

      Gurpreet,
      I’ve contacted the author of PHPLiveX and informed him about the problem with IE6.
      For now it does work with IE7

    • vinu on June 21, 2007

      Gurpreet,
      Arda Beyazoğl (the author of PHPLiveX) was kind enough to go though the script to check the problem. IE6 had a problem with some of the character replacement in the PHPLiveX code. I’ve updated the demo and zip file with the changed versions of the code to make it work on IE.

    • C Bess on September 22, 2007

      I read the post thinking what you just did would be easy… I’m disappointed. You want easy, and a no javascript (literally), check out QuTags. No javascript, means none with QuTags; not one line, not one character of javascript!

    • vinu on September 24, 2007

      thanks for that – I didn’t know about QuTags till you mentioned it

    • JasonDH on July 16, 2010

      I have been using PHPLiveX for some time in a RIA I am developing for our corporation. I have nothing but high praise for this helper framework.

    Leave a Reply