2013-06-24 140 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 –