删除按钮不起作用

时间:2018-02-18 12:39:25

标签: javascript php sql ajax database

TIA.I有两个按钮,一个是Next(#next),它在id(PRIMARY/AI)(已经正常工作)的数据库中一次发布commentbox个按钮。以及Processed(#processed)每次点击时都会从数据库中删除或删除行(commentbox中显示的ID)。我有代码:真的很抱歉,如果它不好,我也使用Ajax:

PS:代码是许多教程的汇编 PS:处理过的按钮(删除)是这里唯一的问题

这是JS:

<script>
    //jQuery code here!
    $(document).ready(function() {
        var commentCount = 0;
        $("#next").click(function() {
        commentCount += 1;
            $("#comments").load("load-comments.php", {
                commentNewCount: commentCount
            });
               $("#processed").removeAttr('disabled');
               $(this).attr('disabled', 'disabled');
        });
        $("#processed").click(function() {
            var element = $("#comments");
            var commentNewCount = element.attr("commentCount");
            var info = 'commentCount=' +commentNewCount;
            // alert(info);
            if(confirm("Are you sure you want to delete this row?")){
                  $.ajax({
                      url: 'deleteuser.php',
                      type: 'post',
                      data: info, 
                      success: function(){
                      }
                  });

                  $("#comments").parent().parent().fadeOut(500, function(){
                  $("#comments").remove();
                  });

            }
            return false;
               });
         $("#next").removeAttr('disabled');
         $(this).attr('disabled', 'disabled');
        });
</script>

下一步按钮中的phpcode:

 <?php
    include 'dbh.php';

    $commentNewCount = $_POST['commentNewCount'];
    $sql = "SELECT * FROM comments LIMIT 1 OFFSET $commentNewCount";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo $row['id'];

        }
    } else {
        echo "There are no queue!";
    }

?>

处理按钮中的phpcode:

    <?php
include 'dbh.php';

$userid =$_POST['id'];

$delete = mysqli_query($con, "DELETE FROM comments WHERE id=$commentNewCount");
if(!$delete){
echo "The queue Are already Delete" ;
}else{
echo "Success";
}
?>

1 个答案:

答案 0 :(得分:0)

在前端,你正在使用

var info = 'commentCount=' +commentNewCount;

虽然在PHP代码中你没有得到它,你应该使用

$commentNewCount =$_POST['commentCount'];

在PHP删除代码中,没有'id'POST值

另外,请注意变量名称,不清楚是否删除评论或用户