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

时间:2015-12-16 16:25:01

标签: javascript php jquery row html-table

我在一个页面上有两个表。顶部的表只有一行,因为DB中的特定表只有一个条目,即每月的Tank。底部的表格显示了数据库中另一个表的内容“uploads”。从底部表中删除项目时,它会按预期从数据库和physical / 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;

});

1 个答案:

答案 0 :(得分:0)

您可以使用appendTo功能将项目附加到正确的表格中。 检查this post了解如何操作。

获得&#34;当前&#34;表格,请查看hasClass

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