2014-02-07 66 views
1

我有兩個選擇按鈕,並希望篩選記錄,當用戶選擇任何選項時,我必須觸發一個事件並將按鈕的值傳遞給控制器​​的操作。我的代碼是在cakephp通過選擇值ajax

視圖

<?php echo $this->Form->create('Employee'); ?> 
    <fieldset> 
     <div class="pure-control-group"> 
     <?php echo $this->Form->input('designation', array("label" => "Designation",'type' => 'select', 'id'=>'DesignationType','options' => $settings,'empty' => 'select'));?> 

     <?php echo $this->Form->input('district', array("label" => "District",'type' => 'select', 'id'=>'districtType','options' => $district,'empty' => 'select'));?> 
     </div> 
    </fieldset> 
<?php echo $this->Form->end(); 

    $this->Js->get('#DesignationType')->event('change', 
    $this->Js->request(array(
     'controller'=>'employees', 
     'action'=>'getByCategory' 
     ), array(
     'update'=>'#success', 
     'async' => true, 
     'method' => 'post', 
     'dataExpression'=>true, 
     'data'=> $this->Js->serializeForm(array(
     'isForm' => true, 
     'inline' => true 
      )) 
     )) 
    ); 
    ?> 
<div id="success"></div> 

控制器

public function getByCategory(){ 
     $design_id =$this->request->data['Employee']['designation']; 
     $district_id =$this->request->data['Employee']['district'];   
     $this->layout = 'ajax'; 
} 

當我點擊指定,然後即時通訊能夠獲得指定的值,但沒能獲得區。而下面的值出現錯誤

未定義的索引:區[APP \ Controller \ EmployeesController.php,第18行]

那麼,我怎樣才能得到區的價值;

+0

嘗試增加'回波$這 - > Js-> writeBuffer();''後$這 - > Js->請求(..'呼叫 – Rikesh

+0

我不會。不要使用已棄用的JS助手,參見[ajax-and-cakephp](http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/)和鏈接的「鏈接下拉示例」,它是正是你在這裏做的 - 只使用jQuery和CakePHP。 – mark

回答

0

嘗試發送數據變量中的form id。即

嘗試這種情況:

$data = $this->Js->get('#YourFormId')->serializeForm(array('isForm' => true, 'inline' => true));  
$this->Js->get('#DesignationType')->event('change', 
$this->Js->request(array(
    'controller'=>'employees', 
    'action'=>'getByCategory' 
    ), array(
    'update'=>'#success', 
    'async' => true, 
    'method' => 'post', 
    'dataExpression'=>true, 
    'data'=> $data 
    )) 

);