如何使用ajax删除按钮删除发布的用户的评论?
的 _form
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 :(得分:2)
当前用户由Yii :: app() - &gt; user-&gt; id提供。
最简单的方法是将logged0in id与注释的用户ID相匹配。但是,我从您的代码中看到您存储的是电子邮件,而不是ID。
你应该
我建议(a)对你来说是最不痛苦的选择。
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
}
}
答案 1 :(得分: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')
);