News

Log In Sponsors
Partner Sites

AJAX without Javascript!



Reading the title you now may have two questions: [b:31oz98hc][list:31oz98hc]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?[/list:u:31oz98hc][/b:31oz98hc] The reason is I have found a real cool [b:31oz98hc][color=darkred:31oz98hc]PHP library to generate AJAX-services in your website without writing a single line of JavaScript.[/color:31oz98hc][/b:31oz98hc] 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: [b:31oz98hc][i:31oz98hc]xajax[/i:31oz98hc][/b:31oz98hc] 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:31oz98hc] <?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 :-) ?> [/code:31oz98hc] Cheers! - leonard

Click here to read the whole forum topic