在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个回答


我最终通过参考 </ p>

在我的提交按钮上,我使用回调函数在数组中添加了'buffer'=> false,因此最终提交如下所示</ p>

  echo $ this-> Js-> submit('Demote Comment',array(
                     'url'=> array(
                     'controller'=>'AwardNominees',
                     'action'=>'load_comments'
                     ),
                 '之前'=> $ this-> Js-> get('#demot')-> effect('fadeIn'),
                 'success'=> $ this-> Js-> get('#demot')-> effect('fadeOut'),
                 'update'=>'#comments',
                 '缓冲区'=>否
                 ));
</ code> </ pre>

,我在创建的表单中添加了不同的ID。 这使我的代码按预期工作。 :-)</ p>
     </ div>

展开原文

原文

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. :-)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐