cakePHP基本的ajax形式问题

时间:2010-02-10 21:16:09

标签: cakephp

我正在尝试做一个简单的表单,使用ajax将新评论添加到博客文章中(实际上这是cakePHP教程的一部分)

但问题是提交按钮什么都不做

这是在 view.ctp 文件中生成表单的代码部分

<?php echo $ajax->form('/comments/add', 'post', array('url' => '/comments/add', 'update' => 'PostsComments', 'indicator' => 'commentSaved'));?>
<fieldset>
    <legend><?php __('Add Comment');?></legend>
        <?php  
            echo $form->hidden('Comment.post_id', array('value' => $post['Post']['id']));
            echo $form->input('Comment.name');
            echo $form->input('Comment.content');                            
        ?>
</fieldset>
<?php echo $form->end('Submit');?>

以下是 comments_controller.php

中的添加评论操作
if (!empty($this->data)) {
    $this->Comment->create();
    if ($this->Comment->save($this->data)) {
        $comments = $this->Comment->find('all',array('conditions'=>array('post_id'=>$this->data['Comment']['post_id']),'recursive'=>-1));
        $this->set(compact('comments'));
        $this->render('add_success','ajax');
    } else {
        $this->render('add_failure','ajax');
    }
}      

问题是动作添加不是从视图中调用的......当我查看生成的html源代码时,我发现了类似的内容

<form id="form304217296" onsubmit="event.returnValue = false; return false;" method="post" action="/php-cake/blog/comments/add"><fieldset style="display:none;">

如果我手动删除了on submit标签,则会调用该操作,但add_success.ctp将作为新页面而不是ajax调用生成。

那么可能是什么问题?

2 个答案:

答案 0 :(得分:3)

最后我发现了问题

教程中有多个错误(CakePHP中的第8章,从新手到专业)修复这些错误后我发现另一个问题是cakephp v1.2.6与最新版本的原型(v1.6.1)不兼容 因为它在firebug中给出了以下错误

Event.observe is not a function

所以我使用了原型的1.6.0.3版本,问题解决了。

有关本章中的错误列表,请参阅this

答案 1 :(得分:0)

您是否在View / Layout中包含了原型和scriptaculus(Ajax-helper都需要这两个)?如果你也使用它也可能会干扰jquery