Cjax Framework


The call() method allows you to make ajax requests to ajax controllers. You can pass multiple parameters to this method.

Call() takes the following parameters

Variable Required Type Options Description
$url Yes Mixed string $url

array Cjax 5.9+
as an array the options are as follow:
string controller, string method, mixed parameters, array options
parameters - can pass a string with one or an array with more element ids, for its value.
options - cache = true/false

This url is fetched with ajax
$container_id No String div, span, p, td, etc
You must echo your desire content from the controller/method.
Update a specific container with this pararement, any text the ajax call comes back with in the response will be automatically propagated to this container.
$confirm No String string $message Need to ask "Are you sure?" Before triggering the call() ?

Examples

Simple Usage
You can use the call() method without any other parameters. It will trigger the ajax request in the instant.
PHP
<?php 

    $ajax
->call("ajax.php?controller/method");

    
//As of 5.9 you can do this way
    
$ajax->call(array("controller","method"));
    
?>
More Usages
You can use the call() method with any of the Cjax trigger events.
PHP
<?php 

    
//the most common usage
    
$ajax->click("#link1"$ajax->call("ajax.php?controller/method"));

    
//used in a text-type field.
    
$ajax->change("#text1"$ajax->call("ajax.php?controller/method"));

    
//used in a text-type field. In keyup event, basically every time you type
    
$ajax->keyup("#text1"$ajax->call("ajax.php?controller/method"));
    
?>
Cjax 5.9+
The call method remains the same with the exception of the url$ argument, which now supports arrays as well as the previous method. As of Cjax 5.9, you can pass an array to the url$ argument specifying a controller and a method, and additional options. Passing an array allows you to enter the controller , method, without specifying the dispatcher file ajax.php.

You can pass a 3rd parameter in the array to pass element values, generelly you would pass these in the url in between pine lines eg. |text1| would send the value of a text field which id is text1. You may pass one parameter, a string or you can pass an array of element, to pass their value. If the element is not found on the page, the raw text will be passed.

PHP
<?php 

    
//pass array
    
$ajax->call(array('controller','method'));

    
//pass array with controller, method and one value
    
$ajax->call(array('controller','method''text1'));
    
?>
In the 3rd example below, we are passing multiple element values, as well as a direct id, that will work just fine.
PHP
<?php 

    
//pass array with controller, method and multiple values
    
$values = array('text1','text2'12345);
    
$ajax->click('#link_values'$ajax->call(array('controller','method'$values));

    
?>

Try it


Test



In the example above, we send text1, text2, and 12345. Since text1 element id exists, it passes its value, and text2 does not exist, it just passes the raw text, all numeric values are passed as they are.
Options / Caching
The $url parameter used as a array as explained in the previous section can support even more parameters.
One of the parameter is $options parameter. Currently $options only have one option called cache.

This cache option, lets you specify whether you want Cjax to cache the current ajax request. The ajax request is cached by saving the url as a unique key, so if you make more than one request to the same url, the response of the first request is always returned. If the url changes, then it will go through the same process. Say if you are sending a different id on within the same ajax request it will cache each one of these calls, the second call to the same url doesn't make it to the server, instead the first response is re-used.
PHP
<?php 

    
//using caching,
    
$ajax->click('#text3'$ajax->call(array('controller','method'null, array('cache' => true)));

    
?>

Try it


Test

Parameters

Container
The call() method contains 3 parameters and on itself, and the first parameter $url can be used in two different ways. As a string and as an array.
This method also has other two parameters $container_id, which allows pass a container id, (a div, span, td, etc) which you would want to be updated with the response of the ajax call. If you intend to update something specific on the page this is a straight forward way to do it. For this to work, you will need to echo the content inside the controller method.

PHP
<?php 

    
//using container
    
$ajax->click('#link_container'$ajax->call(array('call','container'), 'my_div'));

    
?>

Try it


Test


Confirm
The last parameter method call() uses is $confirm. With this youu can ask as user if he is sure he wants to continue, and if the user clicks Ok, the ajax request continues, otherwise it cancels.
PHP
<?php 

    
//using container
    
$ajax->click('#link_confirm'$ajax->call(array('call','confirm'), null,'Are you absolutely sure you want to continue?'));

    
?>

Try it


Test (Click Ok)



Previous Topic:  Previous Class    ·   Top of Page   ·   Demos Home   ·  

CodeIgniter  ·  Copyright © 2006 - 2012  ·  Cjax