xajax: Building Interactive PHP Apps Without the JavaScript Headache
It's late 2004, and the "Web 2.0" revolution is picking up speed. We all want our apps to feel like Gmail or Google Maps, but let's be honest: writing XMLHttpRequest code that works in both Internet Explorer 6 and Mozilla Firefox is a nightmare. This is where xajax comes in. It’s a PHP library that lets you call PHP functions from JavaScript and update the page content directly from PHP.
How it Works
xajax generates the necessary JavaScript for you. When you call an xajax function, it sends a request to the server, executes your PHP function, and then returns a "Response" object containing a list of commands (like "assign this value to this div") that the xajax JavaScript engine executes on the client.
Setting up a Simple Update
First, we define our PHP function and register it with the xajax object.
require("xajax.inc.php");
$xajax = new xajax();
function updateMessage($text) {
$objResponse = new xajaxResponse();
// Directly manipulate the DOM from PHP!
$objResponse->addAssign("content_div", "innerHTML", "You typed: " . $text);
return $objResponse;
}
$xajax->registerFunction("updateMessage");
$xajax->processRequests();
The HTML Side
In your HTML, you just need to include the generated JavaScript and call the function using the xajax_ prefix.
<html>
<head>
<?php $xajax->printJavascript(); ?>
</head>
<body>
<input type="text" id="myInput" onkeyup="xajax_updateMessage(this.value);">
<div id="content_div">Type something above...</div>
</body>
</html>
No More eval()
The beauty of xajax is that it handles the serialization and the DOM updates for you. You don't have to worry about parsing JSON (which isn't even popular yet) or XML manually. You just think in terms of PHP logic. While some purists argue that we should keep our "behavior" (JS) separate from our "logic" (PHP), xajax is a godsend for PHP developers who want to stay productive in a rapidly changing web landscape.
Aunimeda develops websites and web applications for businesses - corporate sites, e-commerce, portals, and custom platforms.
Contact us to discuss your web project. See also: Web Development, E-commerce Development