It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum

AJAX without Javascript!

Moderator: Malcolm

AJAX without Javascript!

Postby leonard on Fri Dec 21, 2007 2:04 am

Reading the title you now may have two questions:
    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
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 7:11 am
Location: Switzerland

Postby knowa on Fri Jan 25, 2008 7:27 am

Wow, thank you very much! That is the solution for a poor php programmer like me who don't know any js ;P
User avatar
knowa
 
Posts: 19
Joined: Sat Dec 08, 2007 4:32 pm
Location: Switzerland


Who is online

Users browsing this forum: No registered users and 0 guests