Javascript PHP将行从一个表移动到另一个表


Javascript PHP move row from one table to another

我在一页上有两个表。顶部的表只有一行,因为DB中的特定表只有一个条目,即月度储罐。下表显示了数据库中另一个表"uploads"的内容。当从底部表格中删除某个项目时,它会按预期从数据库和物理/上传/文件夹中删除该项目。当一个项目从底部表格中选择为月度储罐时,它应该从底部表格消失,这是正确的,然后顶部表格应该被清除,所选的项目应该自动消失在顶部表格中。到目前为止,我已经尝试了很多种方法,但都没有成功。我能让最上面的表刷新的唯一方法就是实际刷新页面。这是我想要的,但我想在不刷新页面的情况下完成。

totm.php(不是整个文件,因为它的大部分工作):

<!--put body stuff here-->
    <div class="content">
        <div class="container-fluid">
            <div class="row">                   
                <div class="col-md-12">
                    <div class="card card-plain">
                        <div class="header">
                            <h4 class="title">Current Tank of the Month</h4>
                            <p class="category">This is your current tank of the month. </p>
                        </div>
                        <div class="content table-responsive table-full-width">
                            <?php
                                // Check connection
                                if ($mysqli->connect_error) {
                                    die("Connection failed: " . $mysqli->connect_error);
                                } 
                                else
                                {
                                    if (!$stmt = $mysqli->query("SELECT * FROM totm")) {
                                        echo "Query Failed!: (" . $mysqli->errno . ") ". $mysqli->error;
                                    }
                                    echo "<table class='table table-hover'>";
                                    echo "<thead>
                                            <th>Image</th>
                                            <th>Filename</th>
                                            <th>Description</th>
                                            </thead>
                                            <tbody>";
                                    while ($row = mysqli_fetch_array($stmt)) {
                                        $i = $row["id"];
                                        $f = $row["file"];
                                        $d = $row["description"];
                                        echo "<tr id='$i' file='$f' desc='$d'>";
                                        echo "<td> <a href='../assets/img/totm/" . $row["file"] . "'><img class='thumbnail' src='../assets/img/totm/" . $row["file"] . "' alt='" . $row["file"] . "' /> </td>";
                                        echo "<td>" . $row["file"] . "</td>";
                                        echo "<td>" . $row["description"] . "</td>";
                                        echo "</tr>";
                                    }
                                    echo "</tbody>";
                                    echo "</table>";
                                    if (mysqli_num_rows($stmt) == 0) {
                                        echo "No records found.";
                                    }
                                } 
                            $stmt->free();
                            //$mysqli->close();    
                            ?>       
                        </div>
                    </div>
                </div>                
            </div>                    
        </div>
        <div class="container-fluid">
            <div class="row">                   
                <div class="col-md-12">
                    <div class="card card-plain">
                        <div class="header">
                            <h4 class="title">Current Entries</h4>
                            <p class="category">Here you will find the current entries for the Tank of the Month contest. Select one as the new Tank of The Month and delete the rest. Keep it clean around here! </p>
                        </div>
                        <div class="content table-responsive table-full-width">
                            <?php
                                // Check connection
                                if ($mysqli->connect_error) {
                                    die("Connection failed: " . $mysqli->connect_error);
                                } 
                                else
                                {
                                    if (!$stmt = $mysqli->query("SELECT * FROM uploads")) {
                                        echo "Query Failed!: (" . $mysqli->errno . ") ". $mysqli->error;
                                    }
                                    echo "<table class='table table-hover'>";
                                    echo "<thead>
                                            <th>Image</th>
                                            <th>Name</th>
                                            <th>Email</th>
                                            <th>IP</th>
                                            <th>Date</th>
                                            <th>Description</th>
                                            <th>Action</th>
                                            </thead>
                                            <tbody>";
                                    while ($row = mysqli_fetch_array($stmt)) {
                                        $i = $row["id"];
                                        $f = $row["file"];
                                        $d = $row["description"];
                                        echo "<tr id='$i' file='$f' desc='$d'>";
                                        echo "<td> <a href='../uploads/" . $row["file"] . "'><img class='thumbnail' src='../uploads/" . $row["file"] . "' alt='" . $row["file"] . "' /> </td>";
                                        echo "<td>" . $row["name"] . "</td>";
                                        echo "<td>" . $row["email"] . "</td>";
                                        echo "<td>" . $row["ip"] . "</td>";
                                        echo "<td>" . $row["date"] . "</td>";
                                        echo "<td>" . $row["description"] . "</td>";
                                        echo "<td>
                                                  <button class='btn btn-round btn-danger deleteitem'>Delete</button>
                                                  <div class='space-20'></div>
                                                  <button class='btn btn-round btn-success chooseitem'>Select</button>
                                              </td>";
                                        echo "</tr>";
                                    }
                                    echo "</tbody>";
                                    echo "</table>";
                                    if (mysqli_num_rows($stmt) == 0) {
                                        echo "No records found.";
                                    }
                                } 
                            $stmt->free();
                            $mysqli->close();    
                            ?>       
                        </div>
                    </div>
                </div>                
            </div>                    
        </div>
    </div>

totm-backend.js:

$(".deleteitem").click(function () {
var parent = $(this).closest('TR');
var id = parent.attr('id');
var file = parent.attr('file');
if (confirm("Are you sure you want to delete this?")) {
    $.ajax({
        type: "POST",
        data: { delete_id: id, delete_file : file },
        URL: "totm.php",
        success: function (msg) {
            parent.fadeOut('slow', function () { $('#' + id).remove() });
        }
    });
}
return false;

});

$(".chooseitem").click(function () {
var parent = $(this).closest('tr');
var id = parent.attr('id');
var file = parent.attr('file');
var desc = parent.attr('desc');
if (confirm("Are you sure you want to promote this to Tank of the Month?")) {
    $.ajax({
        type: "POST",
        data: { promote_id: id, promote_file: file, promote_desc: desc },
        URL: "totm.php",
        success: function (msg) {
            parent.fadeOut('slow', function () { $('#' + id).remove() });
        }
    });
}
return false;

});

您可以使用appendTo函数将项附加到适当的表中。查看这篇关于如何做到这一点的文章。

要获取"当前"表格,请查看hasClass

我只是在学习javascript和jquery,所以可能有更好的方法。