Welcome to the Cookbook

loading...

7.1 AJAX

ada perubahan tertangguh untuk seksyen ini. More information about translations

The AjaxHelper utilizes the ever-popular Prototype and script.aculo.us libraries for Ajax operations and client side effects. To use the AjaxHelper, you must have a current version of the JavaScript libraries from www.prototypejs.org and http://script.aculo.us placed in /app/webroot/js/. In addition, you must include the Prototype and script.aculo.us JavaScript libraries in any layouts or views that require AjaxHelper functionality.

You'll need to include the Ajax and Javascript helpers in your controller:

class WidgetsController extends AppController {
	var $name = 'Widgets';
	var $helpers = array('Html','Ajax','Javascript');
}
  1. class WidgetsController extends AppController {
  2. var $name = 'Widgets';
  3. var $helpers = array('Html','Ajax','Javascript');
  4. }

Once you have the javascript helper included in your controller, you can use the javascript helper link() method to include Prototype and Scriptaculous:

echo $javascript->link('prototype');
echo $javascript->link('scriptaculous'); 
  1. echo $javascript->link('prototype');
  2. echo $javascript->link('scriptaculous');

Now you can use the Ajax helper in your view:

$ajax->whatever();
  1. $ajax->whatever();

If the RequestHandler Component is included in the controller then CakePHP will automatically apply the Ajax layout when an action is requested via AJAX

class WidgetsController extends AppController {
	var $name = 'Widgets';
	var $helpers = array('Html','Ajax','Javascript');
	var $components = array( 'RequestHandler' );
}
  1. class WidgetsController extends AppController {
  2. var $name = 'Widgets';
  3. var $helpers = array('Html','Ajax','Javascript');
  4. var $components = array( 'RequestHandler' );
  5. }