-
Notifications
You must be signed in to change notification settings - Fork 0
XAJAX
Category:Libraries Category:Libraries::AJAX
(Originally at [url=http://www.codeigniter.com/forums/viewthread/493/]http://www.codeigniter.com/forums/viewthread/493/[/url] and [url=http://www.codeigniter.com/forums/viewthread/248/]http://www.codeigniter.com/forums/viewthread/248/[/url])
[url=http://xajax.sourceforge.net]Xajax[/url] 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/library/ folder. Create an application/init/init_xajax.php file:
[code]<?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';
?>[/code]
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()
[b]Note: if you want to put this in your core codeigniter libraries path (ie, system/libraries and system/init), you should change [em]APPPATH[/em] to [em]BASEPATH[/em] in the above code.[/b]
[h3]Example of use[/h3]
Here’s an example controller method showing it working:
[code]class testxajax extends controller { function index() { function test_function($number) { $objResponse = new xajaxResponse(); $objResponse->addAssign("SomeElementId","innerHTML", "Xjax 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);
} }[/code]
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.
[b]Couple of caveats:[/b]
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.