cakephp的2 AJAX形式(cakephp 2 ajax form)

2019-06-23 17:55发布

我无法建设的CakePHP 2 ajax的形式,因为1.3这显然有了很大的变化。

我使用下面的代码:

<div id="commentForm">
<div id="commentStatus"></div>
<?php
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->Js->submit('Save', array('update' => '#commentStatus'));
echo $this->Form->end();
?>

然而按下按钮时,表单不会提交。

我将感谢您的帮助!

谢谢!

Answer 1:

在您的视图文件试试这个:

<?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();

?>

注: #CommentSaveForm是CakePHP所生成的ID,如果你有自己的然后使用



Answer 2:

你想显示加载图像,使用“前”和“完整的” $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();",
   ));
?>


文章来源: cakephp 2 ajax form