Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii创建后删除而不刷新不起作用_Php_Jquery_Ajax_Yii - Fatal编程技术网

Php Yii创建后删除而不刷新不起作用

Php Yii创建后删除而不刷新不起作用,php,jquery,ajax,yii,Php,Jquery,Ajax,Yii,也许我的问题有点棘手,但我看不出问题出在哪里 在我的帖子行动索引中 我选择连接用户的所有帖子和相关评论 $sql = 'SELECT p.id, p.subject, p.user_id, c.user_id c_user_id, c.id c_id FROM post p LEFT JOIN comment c ON p.id = o.post_id

也许我的问题有点棘手,但我看不出问题出在哪里

在我的帖子行动索引中

我选择连接用户的所有帖子和相关评论

$sql = 'SELECT  p.id, p.subject, p.user_id, c.user_id c_user_id,
                        c.id c_id
                FROM post p
                LEFT JOIN comment c ON p.id = o.post_id 
                AND o.user_id=:user_id
                LIMIT 0 , 10 ';
然后,“索引”视图列出所有帖子。 对于连接用户有评论的帖子 此时会出现“取消评论”按钮。 对于连接用户没有评论的帖子, 此时会出现“创建注释”按钮

从显示的角度看,这很好

当用户单击“createcomment”时,我使用ajax运行jquery脚本,以便继续运行 同一页

在我的post-index视图中,我调用comment/delete操作(就像我创建评论时那样,调用 评论/创建操作)

当我单击cancelComment按钮时,我运行以下jquery脚本

$("#cancelComment<?php echo $data['id']; ?>").click(function(e) {

                   var $this = $(this);
                $.ajax({
                    type: "POST",
                    url: "<?php echo Yii::app()->createUrl('comment/delete', array('id' => 
                            $comment_id, 'user_id'=> Yii::app()->user->id)); ?>",
                    success: function(data) {
                       }
                });
            });
    });
});
创建脚本如下所示

public function actionDelete($id, $user_id)
    {
        $model=$this->loadmodel($id);

        if ($model->user_id == Yii::app()->user->id){

        $this->loadModel($id)->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if(!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
    }
$("#createComment<?php echo $data['id']; ?>").click(function(e) {
  var $this = $(this);
  $.ajax({
  type: "POST",
  url: "<?php echo Yii::app()->createUrl('comment/create', array('post_id' =>  $data['id'])); ?>",
  success: function(data) {
           $this.after(data);
           $(".form").slideDown(2000);
}
这是不好的,因为它错过了注释id。 在我看来,这不是一个好的背景

如果我刷新页面,(因此获取与此帖子相关的所有帖子和取消按钮)

当我单击“取消”按钮时,它会工作,因为Url如下所示:

localhost/mysite/index.php/comment/delete/17?user_id=18
添加了我的创建操作注释/创建包含以下内容 公共函数actionCreate($post\u id)

*添加了创建注释视图

<?php

$this->breadcrumbs=array(
    'Comment'=>array('index'),
    'Create',
);
?>

<?php $this->renderPartial('_form', array(
    'model'=>$model, 
    'post_id' =>$post_id,
    )); 
?> 

我的问题是:为什么我在创作后会失去背景

你知道吗


谢谢你的帮助。

你能展示你的
操作创建
功能吗?@Oledje我用创建操作更新帖子你能展示查看
创建
@Oledje我更新帖子吗?你每个帖子只有一条评论吗?
{
        $model=new Comment;
        $model->post_id = $post_id;

        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($model);

        if(isset($_POST['Comment']))
        {
            $model->attributes=$_POST['Comment'];

            if($model->save()) {
                $this->redirect(Yii::app()->request->urlReferrer);

            } else {

                $this->render('create',array(
                    'model'=>$model, 
                    'post_id' => $post_id,
                )); 
            }
        }

        $this->render('create',array(
                    'model'=>$model, 
                    'post_id' => $post_id,
        ));
    }
<?php

$this->breadcrumbs=array(
    'Comment'=>array('index'),
    'Create',
);
?>

<?php $this->renderPartial('_form', array(
    'model'=>$model, 
    'post_id' =>$post_id,
    )); 
?>