- 1) Why the hell is he doing an AJAX-post in the PHP-Forum?
2) The 'J' in AJAX stands for JavaScript, so why does he say it's without JavaScript?
The reason is I have found a real cool PHP library to generate AJAX-services in your website without writing a single line of JavaScript. Cool, eh?
Meaning... no (browser-specific) XMLHTTP-request and not even a JavaScript function to handle the response-XML-object.
The "magic" library is called: xajax and can be downloaded here:
http://sourceforge.net/projects/xajax/
I have written a small demo-example here:
http://devppl.chew.ch/guestbook
There are 3 AJAX services in the guestbook.
1) webServiceShowForm(): this is called by clicking on the "add comment"-button.
2) webServicePreview(): this is called by clicking on the "preview"-button
3) webServiceShowButton(): this is called by clicking on the "close" button
If you check the source-code of the page, you will see the JavaScript-part above the meta-TAG. This JavaScript is fully dynamically generated by the PHP-xajax library.
Here is how it works: (the following is all PHP-code)
- Code: Select all
<?php
require ("xajax.inc.php");
// generate the xajax object:
$xajax = new xajax();
// define the three services:
function webServicePreview($name,$formInput)
{
$AJAX_OUTPUT = // the preview-HTML-code, this is not AJAX-specific, so I don't post it here
// initiate response-object:
$objResponse = new xajaxResponse();
// delete old preview, if exists ("preview") is the HTML-object-ID
// note that this will generate JavaScript later
$objResponse->remove("preview");
// create a div-node with ID="preview" after the "formTable"-ID
// note that this will generate JavaScript later
$objResponse->insertAfter("formTable","div","preview");
// assign the AJAX-output to the preview-ID as innerHTML
// note that this will generate JavaScript later
return $objResponse->assign("preview","innerHTML",$AJAX_OUTPUT);
}
function webServiceShowForm()
{
// function to print the form
}
function webServiceShowButton()
{
// function to print the button
}
// OK, we need some configuration to tell where the javascript-libraries are:
$xajax->configure("javascript URI","/php/xajax/");
// now we register our 3 functions as AJAX-services:
$xajax->registerFunction("webServiceShowForm");
$xajax->registerFunction("webServiceShowButton");
$xajax->registerFunction("webServicePreview");
$xajax->processRequest();
$JAVASCRIPT = $xajax->getJavascript();
// the $JAVASCRIPT variable now contains all the AJAX-JavaScript required for the webpage. Just print it out and all is ready :-)
?>
Cheers!
- leonard

