2013-06-24 41 views
0

嗨,我使用php代碼來查看從我的sql的任何職位的所有評論,我想添加一個提交按鈕給每一個評論,以刪除它,而不刷新頁面我的意思是使用AJAX我不知道如何編寫代碼,並將其與HTML代碼連接我想補充提交這樣的:用AJAX和delete.php頁添加提交刪除評論使用ajax,php

<form> 
<input type="submit" id="deletecomment"> 
</form> 

,並將其連接到刪除評論( AJAX,delete.php)???????

這是我的代碼

$result = mysql_query ("select * from post_comments WHERE link ='$getlink' order by link asc"); 
while ($row = mysql_fetch_array($result)) { 
    $id = $row['id']; 
    $link = $row['link']; 
    $time = $row['time']; 
    $content = nl2br($row['content']); 
    $name = ($row['link'] != '') ? '<h3 style="color:blue">'.$row['name'].'</h3>' : $row['name']; 
    $imge = $row['imge']; 

    echo ' 
     <div class="commentuserbackground"> 
      <img src="'.$imge.'" width="30px" height="30px"> 
      <div id="comment-'.$id.'" class="username1">'.$name.'</div> 
      <div class="username2">'.$content.'</div><div class="commenttime"><h4 style="color:#5a5a5a">'.$time.'</h4> 
     </div></div>'; 
} 

回答

0

如果你已經在HTML中包含了jQuery lib中,你可以做這樣的事情:

# html page 
<button data-id="1" class="delete_comment" /> # no need to wrap in form 

# At the bottom of the body tag 
$(".delete_comment").click(function(e){ 
    e.preventDefault(); 
    var $button = $(this), $comment = $button.parent(), id = $button.data("id"); 
    $.ajax({ 
    type: 'DELETE', 
    url: "/path/to/comment/" + id, 
    success: function(){ $comment.remove(); } 
    }); 
}); 
+0

確定周華健我會把你的HTML代碼在側回聲打印每個評論。好的,我在我的html中使用jquery lib,但是你在第6行中的含義是什麼(「url:path/to/comment /」+ id)什麼是id,因爲每個評論都有它的id –