简体   繁体   English

在div中删除一行

[英]Remove a row inside a div

I want to insert a "remove" button in each of these divs, so that the database's row and the div can be deleted using the remove button. 我想在每个div中插入一个“删除”按钮 ,以便可以使用“删除”按钮删除数据库的行和div

Number of divs vary according to the number of rows in the database. div的数量根据数据库中的行数而变化。

It should appear as follows, 它应该显示如下,

Showing data works just fine. 显示数据效果很好。 But, delete (remove button) doesn't work. 但是,删除(删除按钮)无效。

PHP 的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);


?>

As the discussion from the comment, The following codes given. 作为从评论的讨论,给出以下代码。

Updated HTML: 更新的HTML:

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

Javascript: 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 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! ";
}

If you want to remove the total div as well with the database field then use: 如果要同时删除数据库字段的总div,请使用:

Javascript: 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);  
    });
});

If you want to pass your $userID in each input then use: 如果要在每个input传递$userID ,请使用:

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

Javascript 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);  
    });
});

This is just an answer of your question, but you have to use this as you want. 这只是您问题的答案,但是您必须根据需要使用它。 This may help you, try and let me know what happens. 这可能会对您有所帮助,请尝试让我知道会发生什么。

The remove button doesnt work because you never get into deleteUser() method. 删除按钮不起作用,因为您永远不会进入deleteUser()方法。

You cant just write 你不能写

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

as it was inside a form. 因为它在表格内。 For it to trigger, write it like this: 要触发它,请这样编写:

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

Then, when calling 然后,当打电话

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

Hope this helps. 希望这可以帮助。

<?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