【问题标题】:add submit to delete comment using ajax , php使用ajax,php添加提交以删除评论
【发布时间】:2013-06-20 22:36:56
【问题描述】:

嗨,我使用 php 代码从我的 sql 中查看任何帖子的所有 cmets,我想为每条评论添加一个提交按钮,以便在不刷新页面的情况下删除它,我的意思是使用 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>';
}

【问题讨论】:

    标签: ajax jquery


    【解决方案1】:

    如果您的 html 中已经包含 jquery 库,您可以执行以下操作:

    # 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(); }
      });
    });
    

    【讨论】:

    • 好的,Emil 我会把你的 html 代码放在旁边的 echo 中,以便与每条评论一起打印。好的,我在我的 html 中使用 jquery lib,但是你在第 6 行(“url:path/to/comment/”+id)是什么意思呢,因为每个 cmets 都有它的 id
    猜你喜欢
    • 2021-12-10
    • 1970-01-01
    • 2013-07-29
    • 2014-01-30
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 2019-02-20
    • 2019-10-16
    相关资源
    最近更新 更多