jquery - cakephp 2 ajax 形式

标签 jquery cakephp cakephp-2.x

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

但是,按下按钮时不会提交表单。

我将非常感谢您的帮助!

谢谢!

最佳答案

在您的 View 文件中尝试此操作:

<?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,如果您有自己的 ID,请使用它

关于jquery - cakephp 2 ajax 形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10198137/

相关文章:

jquery - Bootstrap 进度条 : add to the css: width value, 全部在 coffeescript/jQuery 中

javascript - 如何隐藏滚动 spy 中的其他内容而不是滚动到特定位置

php - 如何在 ubuntu 服务器上部署 cakephp 应用程序

php - CakePHP 错误 : Call to a member function find() on a non-object

php - Cakephp模型找到多个 'not like'

javascript - anchor 标记不适用于图像 http ://www. smoothdivscroll.com/demo.html

javascript - 如何在 jQuery 或 javascript 中选择月份后隐藏日历?

jquery - CakePHP 3.x 用于 ajax 请求的 Flash 消息

php - 在 CakePHP 2.x 分页中按计算的 MySQL 字段排序

mysql - CakePHP 2.5.1 中无法将数据从一个表单保存到多个表中