繁体   English   中英

在div中删除一行

[英]Remove a row inside a div

我想在每个div中插入一个“删除”按钮 ,以便可以使用“删除”按钮删除数据库的行和div

div的数量根据数据库中的行数而变化。

它应该显示如下,

显示数据效果很好。 但是,删除(删除按钮)无效。

的PHP

    function deleteUser($connection, $userID){  //  this function calls within the "currentUsers" Function
        $sql2 = "DELETE FROM users_table WHERE user_id = '$userID' ";

        if (mysqli_query($connection, $sql2)) {
            header("Location: main.php");

        } else {
                echo "Error! ";
        }

    }


    function currentUsers($connection){
        $sql1 = "SELECT * FROM users_table ";
        $result1 = mysqli_query($connection, $sql1);

        if(mysqli_num_rows($result1) > 0){
            while($row = mysqli_fetch_assoc($result1)) {
                $userID = $row['user_id'];
                $name = $row['name'];
                $country = $row['country'];

                echo '<div> 
                        <h3>'. $userID. " ". $name. " ". $country. '</h3>
                        <input type = "button" name = "removeButton" value = "Remove" method = "GET">
                     </div>';

                if (isset($_GET['removeButton'])) {
                    deleteUser($connection, $userID);
                }
            }
        }else{
            echo "Currently there are no users!";
        }

        mysqli_close($connection);
    }

    currentUsers($connection);


?>

作为从评论的讨论,给出以下代码。

更新的HTML:

<input type="button" name="removeButton" value="Remove" class="removeBtn">

Javascript:

var userID = "<?php echo $userID;?>";
$(".removeBtn").on("click", function(){
    $.post("page.php", { userID : userID}, function(result){
        if(result == "Success") window.location.href = "main.php";
        else alert(result);  
    });
});

page.php

//need the database connection
$userID = $_POST['userID'];
$sql2 = "DELETE FROM users_table WHERE user_id = '$userID' ";
if (mysqli_query($connection, $sql2)) {
    echo 'Success';
} else {
    echo "Error! ";
}

如果要同时删除数据库字段的总div,请使用:

Javascript:

var userID = "<?php echo $userID;?>";
$(".removeBtn").on("click", function(){
    var __this = $(this);
    $.post("page.php", { userID : userID}, function(result){
        if(result == "Success"){
            __this.closest("div").remove();
            window.location.href = "main.php";
        }
        else alert(result);  
    });
});

如果要在每个input传递$userID ,请使用:

<input data-userid = <?php echo $userID;?> type="button" name="removeButton" value="Remove" class="removeBtn">

Java脚本

$(".removeBtn").on("click", function(){
    var __this = $(this);
    var userID = __this.attr("data-userid");
    $.post("page.php", { userID : userID}, function(result){
        if(result == "Success"){
            __this.closest("div").remove();
            window.location.href = "main.php";
        }
        else alert(result);  
    });
});

这只是您问题的答案,但是您必须根据需要使用它。 这可能会对您有所帮助,请尝试让我知道会发生什么。

删除按钮不起作用,因为您永远不会进入deleteUser()方法。

你不能写

<input type = "button" name = "removeButton" value = "Remove" method = "GET">

因为它在表格内。 要触发它,请这样编写:

<form method="GET">
    <input type = "submit" name = "removeButton" value = "<?php echo $userID;?>">
</form>

然后,当打电话

deleteUser($connection, $_GET['removeButton']);

希望这可以帮助。

<?php 
$connection = mysqli_connect('localhost', 'root', '', 'users');
 function deleteUser($connection, $userID){  //  this function calls within the "currentUsers" Function
        $sql2 = "DELETE FROM users_table  WHERE id = '$userID' ";

        if (mysqli_query($connection, $sql2)) {
            header("Location: main.php");

        } else {
                echo "Error! ";
        }

    }
   function currentUsers($connection){
        $sql1 = "SELECT * FROM maps ";
        $result1 = mysqli_query($connection, $sql1);

        if(mysqli_num_rows($result1) > 0){
            while($row = mysqli_fetch_assoc($result1)) {
                $userID = $row['id'];
                $name = $row['name'];
                $country = $row['country'];

                echo '<div> 
                        <h3>'. $userID. " ". $name. " ". $country. '</h3>
                        <a href="main.php?removeButton='.$userID.'" style="text-decoration: none;" > <input type = "button" value="Remove Button"></a>
                     </div>';
             }
        }else{
            echo "Currently there are no users!";
        }

        mysqli_close($connection);
    }

    if (isset($_GET['removeButton'])) 
    {
        deleteUser($connection, $_GET['removeButton']);
    }

    currentUsers($connection);

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM