PHPLiveX - Tutorial
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:
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:
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.
Loading…
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
?>
Loading…
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




















8 Responses to “PHPLiveX - Tutorial”
By Rehan on May 22, 2007 | Reply
Nice tutorial, but shouldn’t the script above $plx->Run(); be $phplive->Run(); ??
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.
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
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
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.
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!
By vinu on Sep 24, 2007 | Reply
thanks for that - I didn’t know about QuTags till you mentioned it