Skip to content
Derek Jones edited this page Jul 5, 2012 · 18 revisions

Category:Library::AJAX

Nov 6, 2007. For updated references: See forum topic: Xajax 0.5.3 in Code Igniter 1.5.4 Wiki Entry: Xajax perfect setup

Note: This doc is obsolete

(Originally at http://www.codeigniter.com/forums/viewthread/493/ and http://www.codeigniter.com/forums/viewthread/248/)

Xajax is an AJAX library for PHP that allows you to create AJAX functionality without writing javascript. It's very functional and fairly simple to use.

To set this up, put xajax.php (originally called xajax.inc.php - removed the .inc to be more Codeigniter-like) and xajaxResponse.inc.php (I left the .inc in here, since it’s included by xajax.php) in your application/libraries/ folder. Create an application/init/init_xajax.php file:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

if ( ! class_exists('xajax'))
{
     require_once(APPPATH.'libraries/xajax'.EXT);
}

$obj =& get_instance();
$obj->xajax = new xajax();
$obj->ci_is_loaded[] = 'xajax';

?>

You also need to put xajax.js somewhere web-accessable, I used a top-level folder called "/js". You'll need to reference this file when called xajax::getjavascript()

Note: if you want to put this in your core codeigniter libraries path (ie, system/libraries and system/init), you should change APPPATH to BASEPATH in the above code.

Example of use

Here’s an example controller method showing it working:

class testxajax extends controller {
  function index() {
    function test_function($number) {
      $objResponse = new xajaxResponse();
      $objResponse->addAssign("SomeElementId","innerHTML", "Xajax is working. Lets add: ".($number+3));
      return $objResponse->getXML();
    }

    $this->load->library('xajax');

    $this->xajax->registerFunction("test_function");

    $this->xajax->processRequests();

    $template['xajax_js'] = $this->xajax->getjavascript(null, '/js/xajax.js');

    $template['content'] = '<div id="SomeElementId"></div><input type="button" value="test" onclick="xajax_test_function(2);">';

    $this->load->view('template', $template);
  }
}

For this example, you also need view called ‘template’ that echos $xajax_js in the head, and $content in the body. You'll have to modify the path in the getjavascript() call if you put xajax.js somewhere else.

For those of you who don't want to define the function within the code..

If you tried the example above but didn't like how you have to define the function "test_function" within the index() function, you can do that as well by just changing the line:

    $this->xajax->registerFunction("test_function");

to:

$this->xajax->registerFunction(array('test_function',&$this,'test_function'));

Here's the example above, with slightly altered (and cleaner) code:

Class Testajax Extends Controller 
{
    function Testajax()
    {
      parent::controller();
          $this->xajax->registerFunction(array('test_function',&$this,'test_function'));
          $this->xajax->processRequests();
    }
    
    function test_function($number) 
    {
              $objResponse = new xajaxResponse();
          $objResponse->addAssign("SomeElementId","innerHTML", "Xajax is working. Lets add: ".($number+3));
                  return $objResponse->getXML();
    }
    function index() 
    {
        $template['xajax_js'] = $this->xajax->getjavascript(null, '/js/xajax.js');

        $template['content'] = '<div id="SomeElementId"></div><input type="button" value="test" onclick="xajax_test_function(2);">';

        $this->load->view('template', $template);
    
    }
   

}

Couple of caveats:

You have to define the function before you call xajax->registerFunction(), and you have to register all the functions before calling xajax->processRequests(). I was hoping to build processRequests into the init file, but unfortunately it wasn’t possible.

Clone this wiki locally