Skip to content

Xajax perfect setup

Derek Jones edited this page Jul 5, 2012 · 9 revisions

Category:Library::External | Category:Library::Xajax

Introduction

How to install Xajax on codeigniter in 9 steps.

After trying to install Xajax newest version in CI and found a view articles here and on other website that described the process and I did find it a little confusing, so I decide to document my step and tried it a few times before posting here. Also note that this is a combination of explanations that I found here and on other websites. So this is what I did.

Installation

**1.**Download the latest version on Xajax. **2.**After unzipping the downloaded folder, move all files and sub folders under folder xajax_core/ to /application/libraries **3.**Rename the file xajax.inc.php to xajax.php to look more like a codeigniter file. **4.**Create the folder /application/init and place a newly created that you should call: init_ajax.php

//example of init_ajax 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';

?>

5. Copy the folder xajax_js/ and all its contents to the main root folder of your web application (example httpdocs/), and do not change the folder name. 6. OPTIONAL - If you are using mod_rewrite which was my case, add the xajax_js in your .htaccess rule to look something like this:

RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|img|css|public|tmp|download|javascript|rte|document|xajax_js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L] 

7. If you need to test it, create a controller testxajax and write this code inside```php <?php class Testxajax extends Controller {

function Testxajax()
{
  parent::controller();
        $this->load->library('xajax');
      $this->xajax->registerFunction(array('test_function',&$this,'test_function'));
      $this->xajax->processRequest();
}

function test_function($number)
{
          $objResponse = new xajaxResponse();
      $objResponse->Assign("SomeElementId","innerHTML", "Xajax is working. Lets add: ".($number+3));
              return $objResponse;
}
function index()
{
    $template['xajax_js'] = $this->xajax->getJavascript(base_url());

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

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

}

} ?>

**8.** Then create a index file in the view folder and place this inside, in my case I place it under the template folder in my view folder.
```php
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Xajax 0.5 test&lt;/title&gt;
    &lt;?=$xajax_js?&gt;
&lt;/head&gt;

&lt;body&gt;
  <h1>&lt;?=$content?&gt;</h1>
&lt;/body&gt;
&lt;/html&gt;

9. And you are done you should be able to view a working example of xajax.

Notes

The other folders under the zip folder that you downloaded from the xajax website are optional. If you do decide to use them copy and place them under you root folder and do not forget to add them in your .htaccess rule if you use mod_rewrite.

If anyone has a better method please update this posting, try to keep in simple so replication under different environments can be done without a lot of hassle.

Clone this wiki locally