我在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();
?>
但是按下按钮时未提交表格。
我会感谢任何帮助!
谢谢!
答案 0 :(得分:11)
在您的视图文件中尝试此操作:
<?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,那么
答案 1 :(得分: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();",
));
?>