dongmeba4877 2013-10-21 13:17
浏览 26

从jQuery调用CakePHP函数的示例

I am new to CakePHP and I am trying to figure you how to make an asynchronous call from a CakePHP view to a function in the controller. I would like the controller function to return a string and have the view display this string. I would also like to to do this without using helpers. I have been trying to find examples on the web but have been unable to do so. Does anyone have a simple example? I am also using jQuery.

Thanks

  • 写回答

1条回答 默认 最新

  • dongqing5925 2013-10-21 15:26
    关注

    CakePHP has a built-in JS Helper to help write aJax functions. The only catch is to include jquery in your head or cake will throw jQuery errors. Heres more information http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html

    Your Form:

    <?php 
      echo $this->Form->create('User', array('default'=>false, 'id'=>'YourForm'));
      echo $this->Form->input('username');
      echo $this->Form->submit('Check Username');
      echo $this->Form->end();
    ?>
    

    The Ajax Function: ('update'=>'#na') is the id of the element you want to update in your view.

    <?php
      $data = $this->Js->get('#YourForm')->serializeForm(array('isForm' => true, 'inline' => true));
      $this->Js->get('#YourForm')->event(
        'submit',
        $this->Js->request(
          array('action' => 'checkUsername', 'controller' => 'user'),
          array(
            'update' => '#na',
            'data' => $data,
            'async' => true,    
            'dataExpression'=>true,
            'method' => 'POST'
          )
        )
      );
      echo $this->Js->writeBuffer();                                                 
    ?>
    

    The Function in User Controller

    function checkUsername(){
      $this->autoRender = false;
    
      $username = $this->User->find('first', array('conditions'=>array('User.username'=>$this->request->data['User']['username'])));
    
      if ( $username == true ) 
        echo 'Username is taken';
      else
        echo 'Username is not taken';  
    }
    

    EDIT** *If you want to use jQuery to do this and not the CakePHP Helper you can use aJax to call an action, then update your element like below*

    $('#element').on('click', function() {
      $.ajax({
          url : '/controller/action',
          type: 'POST',
          success : function(response){
            $('#elementToUpdate').html(response);
          }
      });
    

    } });

    In your Controller Action you can return the "string" you would like to show in the view

    function action(){
      $string = 'Show this in the view';
    
       return $string;
    }
    

    The above example would be executed when you "Click" an element with an id of "element" then upon "Success" would change element with id of "elementToUpdate" to the String "Show this in the view" Since it was returned from the controller action.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错