2014-03-13 99 views
0

你好,我是使用CakePHP我试图做一个链接选择使用Ajax, 的想法是,我让事件jQuery的电平变化行动中的选择输入的链选择使用CakePHP和Ajax

onchange="TheFunction()"; 

但似乎我有一些语法问题,我的想法是,我将所选项目的值发送到ShowProvider函数,该函数搜索属于所选类别的提供程序,然后将其发送回回调函数。

echo $ this-> Form-> input('category_id',array('empty'=>'Selectionnez une','options'=> $ categories,'class'=>'category','onchange' =>”

<?php 
      echo " 

      $.get(
      //premier argument, le lien vers lequel on envoie les données 
      '. $this->Html->url(array('controller'=>'articles','actions'=>'ShowProvider'),true) .', 
      //2eme argument, on envoie l\'id de la catégorie selectionné 
      {id:$("select.category option:selected").attr("value")}, 

      3éme argument 
      function(data){ 
       alert("wssel"); 
      } 
      ) 
      " 
     ?> 
    ')); 

这里是行动ShowProcider()

public function ShowProvider(){ 
    if ($this->request->is('ajax')) { 
     $result = $this->Article->Provider->find('list', 
      array('conditions'=> 
        array('category_id'=>$this->request->query['id']) 
      ) 
     ); 
     if ($result) { 
      $ret['name']=$result['Provider']['name']; 
     } 
     else{ 
      $ret['name']='Vide.'; 
     } 
     echo json_encode($result); 
     exit(); 
    } 
} 

感谢的提前:)

+0

这看起来好像有点 “自建”。使用框架JsonView你可以使这个更干净。另见http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/这里也有一个实例。 – mark

+0

谢谢你的帮助,我会试试 –

+0

https://gist.github.com/taleeb35/66abaae65ccd2d01a2c1 –

回答

0

补充一点:

$this->layout = 'ajax'; 
$this->autoRender = false; 

中庸之道前

public function ShowProvider(){ 

,并删除

exit();