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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序