使用ajax,php添加提交以删除评论

时间:2013-06-24 12:12:08

标签: ajax jquery

嗨,我使用php代码查看我的sql中任何帖子的所有评论,我想为每个评论添加一个提交按钮,以便在不刷新页面的情况下删除它,我的意思是使用AJAX我不知道知道如何编写代码并将其与html代码连接,我希望像这样添加提交:

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

并将其与AJAX和delete.php页面连接以删除评论(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>';
}

1 个答案:

答案 0 :(得分: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(); }
  });
});