【问题标题】:AjaxSubmitButton in Yii framework does not saveYii 框架中的 AjaxSubmitButton 不保存
【发布时间】:2014-02-02 15:43:35
【问题描述】:

提交后页面不刷新成功后,我的ajaxSubmitButton没有保存数据。当我单击该按钮时,它会删除输入字段的内容,但不会保存数据。尽管有错误和成功参数,但不显示任何消息。

在我的表单提交中,提交按钮的代码如下

<?php echo CHtml::ajaxSubmitButton(
      'Save',
      Yii::app()->createUrl('post/viewComment'),
      array(
           'success'=>'js:function(data){
                    alert("commentSubmitted");}',
           'error'=>'js:function(data){
                    alert("comment NOT Submitted");}',  
            )
    ); 
 ?>

我的控制器动作代码是

public function actionViewComment()
    {   
        $post=$this->loadModel();
        $comment=$this->newComment($post);

        $this->renderPartial('_viewComment',array(
            'model'=>$post,
            'comment'=>$comment,
        ));

viewComment 视图,显示 _view 视图列出 cmets,最后显示 _form 视图让用户输入新的评论

newComment 函数包含以下内容

protected function newComment($post)
    {
        $comment=new Comment;
        if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
        {

            echo CActiveForm::validate($comment);
            Yii::app()->end();
        }
        if(isset($_POST['Comment']))
        {
            $comment->attributes=$_POST['Comment'];
            if($post->addComment($comment))
            {
                if($comment->status==Comment::STATUS_PENDING)
                    Yii::app()->user->setFlash('commentSubmitted','Thank you for your comment. Your comment will be posted once it is approved.');
                   $this->refresh();
            }
        }
        return $comment;
    }

实际上,我使用的是 Yii Framework 的博客演示,我认为这是学习 Yii Framework 的一个很好的起点。

首页显示帖子列表。 (它会渲染部分 _view.php。)

我创建了 ajax 按钮,以便能够列出与给定帖子相关的评论并在表单底部显示新评论的输入表单。

我添加了第二个带有“经典提交”的按钮

<?php echo CHtml::submitButton($model->isNewRecord ? 'Submit' : 'Save'); ?>  

它有效,但将我重定向到 cmets 列表,这是我想要避免的; 通过使用 AjaxsubmitButton。当我将光标放在经典按钮或 Ajax 按钮上时,链接是相同的...../blog/index.php/post/viewComment?id=...

显示如下

发布 1

发布 2

 comment 1

 comment 2

 comment 3

添加评论

Input Field

Ajax提交按钮


发布 3

当我点击 ajaxsubmit 按钮时,我有以下内容

发布 1

发布 2

 comment 1

 comment 2

 comment 3

添加评论

Input Field

Ajax提交按钮



添加评论

Input Field

Ajax提交按钮


发布 3

所以我在第一个表单窗口下方得到了第二个表单窗口。并且评论没有保存。

提前感谢您的帮助

【问题讨论】:

  • 你也可以添加你的控制器代码吗?
  • 我认为您正在控制器中处理 $_POST 数据。所以你必须在你的 ajax 选项数组中提到 POST。
  • @kumar_v 我刚刚在问题中添加了控制器代码
  • @kumar_v 。谢谢库马尔,但它仍然无法正常工作。我在帖子中添加了解释。您会看到我添加了一个经典按钮并且保存完成。 Bur 我想让 ajax 按钮按照帖子中的说明工作。
  • 你试过我的解决方案了吗?

标签: php ajax yii


【解决方案1】:

您正在处理来自 POST 的数据。但是你没有在代码中提到。

<?php echo CHtml::ajaxSubmitButton(
      'Save',
      Yii::app()->createUrl('post/viewComment'),
      array(
           'type'=>'POST',
           'dataType'=>'json',
           'success'=>'js:function(data){
                    alert("commentSubmitted");}',
           'error'=>'js:function(data){
                    alert("comment NOT Submitted");}',  
            )
    ); 
 ?>

【讨论】:

    猜你喜欢
    • 2012-06-20
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    相关资源
    最近更新 更多