Archive for November, 2007

TYPO3 and xajax quick guide

Tuesday, November 20th, 2007

Ajax is definetly the thing to go with the web as it enhance the user experience as well as allowing more logic to be on the server side (okey, your pages javascript usualy tend to get slightly more bloated as well..).

Anyway. It’s not ajax in general today, but the good looking PHP/ajax combination found with the xajax project. Xajax allows you to have PHP functions that you mapp/register as javascript calls. There is an extension for TYPO3 called – xajax! You guessed it! There are two tutorial/example extensions as well that describes how it works. But for my memory I’m writing a short guide here as well with my own words. Maybe it helps someone else.

Using ajax in your backend module:

  • Do the obvious. Install the xajax extension from TER (ie TYPO3 Extension Repository).
  • Launch your favourite editor and open your module file like – /typo3conf/ext/myext/mod1/index.php
  • Include the ajax class
  1. // DEFAULT initialization of a module [BEGIN]unset($MCONF);require_once(‘conf.php’);require_once($BACK_PATH.’init.php’);
  2. require_once($BACK_PATH.‘template.php’);
  3. // include XAJAX class library
  4. require (t3lib_extMgm::extPath(‘xajax’) . ‘class.tx_xajax.php’);
  5. $LANG->includeLLFile(‘EXT:myext/mod1/locallang.xml’);
  • Somewhere in your init() method register your PHP function
  1. $this->xajax->registerFunction(array(‘xajaxTest’, &$this, ‘xajaxTest’));
  • In your printContent() method add this to ensure that xajax does whatever it wants (ie handle requests..) first
  1. $this->xajax->processRequests();
  • Then create your nifty PHP function where you want to do magic, and add a responce object like this:
  1. function xajaxTest() {
  2. $c = "ohlas!";// Instantiate the tx_xajax_response object
  3. $objResponse = new tx_xajax_response();
  4. $objResponse->addAssign("mydiv","innerHTML", $c);//return the XML response generated by the tx_xajax_response object
  5. return $objResponse->getXML();
  6. }
  • Now finally add a call to your function. This is probably done in moduleContent() or referenced functions
  1. <a href="#" onClick="xajax_xajaxTest();">xajax magic</a>

Note that we got an extra xajax_ added to the beginning of our function which is added automatically.

That’s it folks!