0

I have two tables on one page. The table at the top only has one row, because that particular table in the DB will only ever have one entry, the Tank of the Month. The bottom table shows the contents of another table in the database, 'uploads'. When an item is deleted from the bottom table, it deletes it from the DB and from the physical /uploads/ folder as expected. When an item is chosen from the bottom table as Tank of the Month, it is supposed to disappear from the bottom table, which it does correctly, then the top table is supposed to clear and the item that was chosen should automatically fade into the top table. I have tried many, many ways of doing this with no luck so far. The only way I can get the top table to refresh is by actually refreshing the page. This does what I want, but I want to do it without the page refresh.

totm.php (not the whole file as most of it works):

<!--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;

});

wogsland
  • 9,106
  • 19
  • 57
  • 93
ckozma
  • 49
  • 6
  • What jquery have you tried to move the table row from the bottom table to the top? – Bob Nocraz Dec 16 '15 at 16:37
  • Well, I think that I can do something like this: current.fadeOut('slow'), function () { $(this).closest('tr').empty() }); to clear the top table, and then current.fadeIn('slow', function () { $('#' + id).insert() });. The problem is how to define the 'current' table, which is the top one. – ckozma Dec 16 '15 at 16:46
  • I cant use var closest = $(this).closest('tr');, and I don't know how many .parents I would have to use, it is very confusing to get the 'current' variable. – ckozma Dec 16 '15 at 16:51
  • https://api.jquery.com/detach/ might give you a bit of insight into how to handle moving the tr from one table to another. – Bob Nocraz Dec 17 '15 at 11:06

1 Answers1

0

You could use the appendTo function to append the item into the proper table. Check this post out on how to do it.

To get the "current" table, check out hasClass

I'm just learning javascript and jquery myself so there may be better ways.

Community
  • 1
  • 1
Keypunch
  • 26
  • 5