PHPLiveX - Tutorial

Written on May 18, 2007 | by vinu |

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.

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:
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:

Get Server Time

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.



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:

include 'PHPLiveX.php';

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

$phplive = new PHPLiveX("myServerTime"); //Register the function with PHPLiveX
?>



Get Server Time





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

Bookmark and share: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • bodytext
  • StumbleUpon
  • BlinkList
  • blogmarks
  • Furl
  • Slashdot
  • Spurl
  • Technorati
  • YahooMyWeb
  • description
  • e-mail
  • Facebook
  • Google
  • IndianPad
  • Live
  • Ma.gnolia
  • NewsVine
  • Reddit
  • TwitThis
  1. 8 Responses to “PHPLiveX - Tutorial”

  2. By Rehan on May 22, 2007 | Reply

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

  3. By vinu on May 22, 2007 | Reply

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

  4. By Gurpreet on Jun 19, 2007 | Reply

    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 :(

  5. By vinu on Jun 20, 2007 | Reply

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

  6. By vinu on Jun 21, 2007 | Reply

    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.

  7. By C Bess on Sep 22, 2007 | Reply

    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!

  8. By vinu on Sep 24, 2007 | Reply

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

  1. 1 Trackback(s)

  2. May 21, 2007: developercast.com » Vinu Thomas’ Blog: PHPLiveX - Tutorial

Post a Comment

About Me

Here's my blog on stuff I keep finding on the Web. More

Want to subscribe?

 Subscribe in a reader Or, subscribe via email:
Enter your email address:  
On the Go? Get this on your Widsets: Add to my Widsets
Find entries :