weixin_33696822 2014-04-22 14:39 采纳率: 0%
浏览 22

在CakePHP中进行Ajax调用

I am trying to make an Ajax call in CakePHP. The first call is working fine and it renders a view with another Ajax submit button. The code below is in first view:

echo $this->Js->submit('LOAD COMMENTS',array(
                'url'=>array(
                    'controller'=>'Nominees',
                    'action'=>'load_comments'
                    ),
                'before'=>$this->Js->get('#sending')->effect('fadeIn'),
                'success'=>$this->Js->get('#sending')->effect('fadeOut'),
                'update'=>'#comments'
                ));
echo $this->Form->end();

In the Nominees controller i have

public $components = array('RequestHandler');
/*Load comments for the nominee using ajax*/
public function load_comments(){

    if(!empty($this->request->data)){

        /* Demoting Irrelevant comments.
Only triggered if the demote comment button is clicked.*/
        if(isset($this->request->data['Nominee']['comment_nominee_id'])){
            $comment_id = $this->request->data['Nominee']['comment_nominee_id'];
            $this->Nominee->updateAll(
            array('Nominee.status' =>0),
            array('Nominee.id'=>$comment_id));
        }

        $userNumber = $this->request->data['Nominee']['number'];
        $award_id = $this->request->data['Nominee']['award'];
        $dt = date('Y');
        $nominationInfor = $this->Nominee->find('all',array(
                            'conditions' => array('Nominee.reg_number'=>$studNumber,'Nominee.year'=>$dt,'Nominee.award_id'=>$award_id,'Nominee.status'=>1),
                            'recursive' => -1
                            ));
        if(empty($nominationInfor)){
            $this->Session->setFlash(__('No Nomination Information Available.'));
            $this->redirect(array('action' => 'choose_category'));
        }
        $this->set(compact('nominationInfor','userNumber','award_id'));

        if($this->request->isAjax()){

            $this->render('load_comments','ajax');
        }

    }
}

Load_comments, being the view thus being called, has an Ajax button which has the view code shown below:

echo $this->Js->submit('Demote Comment',array(

                'url'=>array(
                    'controller'=>'AwardNominees',
                    'action'=>'load_comments'
                    ),
                'before'=>$this->Js->get('#demot')->effect('fadeIn'),
                'success'=>$this->Js->get('#demot')->effect('fadeOut'),
                'update'=>'#comments'
                ));

            echo $this->Form->end();

When the Demote Comment button is clicked,

debug($this->request->isAjax());

returns false, but if LOAD COMMENTS is clicked

debug($this->request->isAjax());

returns true. The idea is when Load Comments is clicked, the comments are loaded then when Demote Comment is clicked(which is the button against a comment) i wanted to make an Ajax call removing the comment thereof. Your help is greatly appreciated.

  • 写回答

1条回答

  • Memor.の 2014-04-23 14:01
    关注

    I finally figured out where i was going wrong with reference to this

    On my submit buttons i added 'buffer'=>false in the array with my callback functions thus the final submit is as shown below

                    echo $this->Js->submit('Demote Comment',array(
                        'url'=>array(
                        'controller'=>'AwardNominees',
                        'action'=>'load_comments'
                        ),
                    'before'=>$this->Js->get('#demot')->effect('fadeIn'),
                    'success'=>$this->Js->get('#demot')->effect('fadeOut'),
                    'update'=>'#comments',
                    'buffer' => false
                    ));
    

    and i put different id to the forms that i created. This got my code working as expected. :-)

    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多