2015-09-04 32 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 
    } 
}