cakephp 2 ajax form

可紊 提交于 2019-11-29 11:25:08

Try this in your view file:

<?php

    $data = $this->Js->get('#CommentSaveForm')->serializeForm(array('isForm' => true, 'inline' => true));
    $this->Js->get('#CommentSaveForm')->event(
          'submit',
          $this->Js->request(
            array('action' => 'save'),
            array(
                    'update' => '#commentStatus',
                    'data' => $data,
                    'async' => true,    
                    'dataExpression'=>true,
                    'method' => 'POST'
                )
            )
        );
    echo $this->Form->create('Comment', array('action' => 'save', 'default' => false));
    echo $this->Form->input('Comment.comments_name');
    echo $this->Form->input('Comment.comments_email');
    echo $this->Form->input('Comment.comments_text');
    echo $this->Form->end(__('Submit'));
    echo $this->Js->writeBuffer();

?>

NOTE: #CommentSaveForm is ID generated by CakePHP, If you have your own then use that

eXi

You want to show the loading image, use 'before' and 'complete' in $this->Js->request():

<?php
    $this->Js->request(array('action' => 'save'), array(
       'update' => '#commentStatus',
       'data' => $data,
       'async' => true,    
       'dataExpression' => true,
       'method' => 'POST',
       'before' => "$('#loading').fadeIn();",
       'complete' => "$('#loading').fadeOut();",
   ));
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!