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

Category:Libraries Category:Libraries::AJAX

TinyAjax is a small php5 library that allows you to easily add AJAX-functionality to existing pages and create new AJAX-enabled pages with just a few lines of code.

  • Put the include/TinyAjax.php and TinyAjaxBehavior.php into system/application/libraries/
  • Put the include/TinyAjax.js into the "top-level directory"/js directory
  • Put the following code into system/application/init/init_tinyajax.php
<?php if(!defined('BASEPATH')) exit('No direct script access aellowed');

if (!class_exists('TinyAjax'))
{
    define('TINYAJAX_PATH', BASEPATH.'application/libraries/');
    require_once(TINYAJAX_PATH.'TinyAjax'.EXT);
}

$obj =& get_instance();
$obj->tinyajax = new TinyAjax();
$obj->tinyajax->setScriptPath('../../js');
$obj->tinyajax->setRequestType('post');

?>

Example of use

A simple multiplication example. Here is the Controller.

<?php
class Ajax extends Controller {
    
    function Ajax()
    {
        parent::Controller();
        $this->load->library('tinyajax');
    }    

    function ajax_multiply($x, $y)
    {
        return $x*$y;
    }        
           
    function multiply()
    {    
        $this->tinyajax->showLoading();
        $this->tinyajax->exportFunction("ajax_multiply", array("first_id", "second_id"), "#third_id", $this);
        
        $this->tinyajax->process();
        $this->load->view('ajax_multiply');
    }    
}
?>

And here is the ajax_multiply.php views file:

<html>
<head>
<? $this->tinyajax->drawJavaScript(false,true); ?>
</head>
<body>
    Multiply:<br>
    &lt;input type="text" id="first_id" value="2"&gt; *
    &lt;input type="text" id="second_id" value="3"&gt; =
    &lt;input type="text" id="third_id" value=""&gt; 
    &lt;input type="button" value=" * " onclick="ajax_multiply()"&gt;
&lt;/body&gt;
&lt;/html&gt;

Example with Behavior

Add this code to the controller:

    
    function ajax_multiplyb($x, $y) 
    {
      $res = $x * $y;
      $res_text = "Multiplying $x and $y results in $res";
    
      $tab = new TinyAjaxBehavior();
      $tab->add(TabSetValue::getBehavior("third_id", $res));
      $tab->add(TabInnerHtml::getBehavior("result_div", $res_text));
      return $tab->getString(); 
   }

    function multiplyb()
    {    
        $this->tinyajax->showLoading();
        $this->tinyajax->exportFunction("ajax_multiplyb", array("first_id", "second_id"), null, $this);
        
        $this->tinyajax->process();
        $this->load->view('ajax_multiplyb');
    }

And the views file ajax_multiplyb.php look like this:

&lt;html&gt;
&lt;head&gt;
&lt;? $this->tinyajax->drawJavaScript(false,true); ?&gt;
&lt;/head&gt;
&lt;body&gt;
    Multiply:<br>
    &lt;input type="text" id="first_id" value="2"&gt; *
    &lt;input type="text" id="second_id" value="3"&gt; =
    &lt;input type="text" id="third_id" value=""&gt; 
    &lt;input type="button" value=" * " onclick="ajax_multiplyb()"&gt;
    <br/>
    <div id="result_div">&nbsp;</div>
&lt;/body&gt;
&lt;/html&gt;
Clone this wiki locally