2014-02-07 68 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 
    )) 

);