Populate Select box dynamically by ajax in Cakephp
To populate a select box by cakephp ajax functionality.
Step 1.
In the end of your view (.ctp)file.This is your javascript code in cake.
<?php
$this->Js->get('#select1')->event('click',
$this->Js->request(array('controller'=>'ControllerName','action'=>'ajax_call'), array(
'update'=>'#select2',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true))
))
);
?>
Step 2.
In your controller file create function with that returns options string
public $helpers = array('Js'); //load Js helper
public function getBySubject(){
$this->autoRender=false; //prevent to load default layout
$option;
//your code
printf($option);
}
step 3.
In the end or your layout file after your loaded scripts
<?php
if (class_exists('JsHelper') && method_exists($this->Js, 'writeBuffer')) echo $this->Js->writeBuffer();
// will write your cached scripts
?>