删除功能无法删除php ajax表中的行

时间:2019-08-23 10:19:32

标签: php ajax row

我想通过单击删除按钮删除一行。

我已经完成了对行中数据的编辑。我想删除行中的数据。删除操作无效。

  db.php
    <script>
$(document).ready(function() {
    $.ajax({
        url: "View_ajax.php",
        type: "POST",
        cache: false,
        success: function(dataResult){
            $('#table').html(dataResult); 
        }
    });
    $(document).on("click", ".delete", function() { 
        var $ele = $(this).parent().parent();
        $.ajax({
            url: "delete_ajax.php",
            type: "POST",
            cache: false,
            data:{
                id: $(this).attr("data-id")
            },
            success: function(dataResult){
                var dataResult = JSON.parse(dataResult);
                if(dataResult.statusCode==200){
                    $ele.fadeOut().remove();
                }
            }
        });
    });
});
</script>



delete_ajax.php
    <?php
        include "php/dbConfig.php";
        $id=$_POST['id'];
        $sql = "DELETE FROM `task` WHERE id=$id";
        if (mysqli_query($conn, $sql)) {
            echo json_encode(array("statusCode"=>200));
        } 
        else {
            echo json_encode(array("statusCode"=>201));
        }
        mysqli_close($conn);
    ?>

view_ajax.php
<td><input type='button' onclick="delete_user('<?php echo $row['id'];?>');"
            data-id="<?=$row['id'];?>"
            data-firstname="<?=$row['firstname'];?>"
            data-lastname="<?=$row['lastname'];?>"
            data-username="<?=$row['username'];?>"
            data-password="<?=$row['password'];?>"
            "></button></td>

by clicking the delete button i want the entire row to be deleted.

删除功能不起作用。请帮助我。

2 个答案:

答案 0 :(得分:0)

尝试在html标签中添加delete

<input class="delete" type='button' ... >

答案 1 :(得分:0)

Js找不到删除的CSS类,在任何位置添加到元素

<element class='delete' >remove </element>