2012-04-17 63 views
4

我无法在cakephp 2中构建一个ajax表单,从1.3开始,它显然发生了很大的变化。cakephp 2 ajax表格

我用下面的代码:

<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

我不知道它是否相关,但你关闭div #commentForm – laxris 2012-04-17 20:00:33

回答

10

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

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

?> 

注:由CakePHP的产生#CommentSaveForm ID,如果你有你自己的,然后使用该

+0

其工作..谢谢..但是当ajax请求正在处理中,我想显示加载图像..你能告诉我如何显示它? – 2012-11-23 06:34:42

+0

如何调用jquery验证? – 2014-01-27 06:08:24

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