cakephp 2 ajax形式 [英] cakephp 2 ajax form

查看:165
本文介绍了cakephp 2 ajax形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在cakephp 2中构建一个ajax表单,这显然自从1.3以来已经改变了很多。

I'm having trouble building an ajax form in cakephp 2 which obviously has changed a lot since 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();
?>

我将非常感谢任何帮助!

I will be thankful for any help!

谢谢! b $ b

推荐答案

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

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

?>

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

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

这篇关于cakephp 2 ajax形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆