2015-09-04 52 views
0

如何才能把ajax删除按钮删除发布用户的评论?
_formAjax删除yii

echo '<div><h3><b><u>Comments</u></b></h3></div>'; 

$commentList = Comments::model()->findAllByAttributes(array('offereventid'=>$id)); 
    foreach($commentList as $Listdata2) 
    { 
     $usercomment = $Listdata2['comment']; 
     $usercommentid = $Listdata2['id'];   
     $usercomtname = $Listdata2['name'];   
     $usercommentmail = $Listdata2['email'];  
     echo '<div><span class="name1">'.$usercomtname.':</span> '.'<span class ="email1">'.'['.$usercommentmail.']'.'</span>'.'</div>';    
     echo '<div class = "cmnts" >'.'"'.$usercomment.'"'.'['.$usercommentid.']'.'</div>'; 
     // echo CHtml::ajaxSubmitButton('Delete ', array('delete', 'id'=>$usercommentid)); 

     echo '<hr>';  
    } 

请帮我一下吧。 我尝试了很多方法,但是当我尝试任何用户都可以删除任何用户的评论。

+0

你不应该写的模型查询问题的解决方案在你看来。 – Criesto

+0

我该如何实现删除功能? –

+0

据我所知你只想在当前用户创建的评论中显示ajaxSubmitButton,并给予当前用户删除的机会。这样对吗? –

回答

1

我得到了

echo CHtml::ajaxButton(
    'Delete', 
    CHtml::normalizeUrl(array(
     'Comments/del/id/' . $usercommentid, 
     'render' => true 
    )), 
    array(
     'dataType' => 'json', 
     'type' => 'post', 
     'success' => 'function(data) { 
      $("#name_"+data).hide(); 
     }', 

    ), 
    array('id' => $usercommentid, 'class' => 'btn btn-success') 
); 
2

当前用户由Yii :: app() - > user-> id给出。

最简单的方法是将记录的ID与评论的用户ID匹配。不过,我从您的代码中看到,您正在存储电子邮件而不是该ID。

因此,应该要么

  • 一)创建并存储用户ID的意见表
  • 二)改变的UserIdentity添加用户的电子邮件

我建议(一)作为你最不痛苦的选择。

if (Yii::app()->user->id == $Listdata2['user_id']) { 
    echo CHtml::ajaxSubmitButton('Delete ', array('delete', 'id' => $idComment)); 
} 

你应该再创建一个控制器动作(我已经离开了明显的错误检查)

function actionDelete($id = null) { 

    // Load the comment object 
    $commentModel = Comment::model()->findByPK($id);eck if the user has 
    // TODO: Do error check here 

    // Check if the user has access to do this. 
    if (Yii::app()->user->id !== $commentModel->user_id) { 
     // TODO: Nice error here. 
     echo "This is not your comment. You cannot delete it"; 
    } else { 
     $commentModel->delete(); 
     // TODO: Error checks here 
    } 
}