Button to remove table row is not getting added to html table

Viewed 32

I am trying to add a button to remove the table row when clicked. This should be placed next to data that is outputted from MySQL database.For some reason the button is not outputting, below is the code for my table:

<table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Subject</th>
                        <th>Message</th>
                        <th>Have you replied?</th>
                    </tr>
                </thead>



                <?php //makes a connection to the database
                $conn = mysqli_connect("localhost", "root", "", "messages");
                if ($conn->connect_error) {
                    die("Connection Failed:" . $conn->connect_error);
                }

                $sql = "SELECT * FROM tbl_messages";
                $result = $conn->query($sql);


                if ($result->num_rows > 0) { //fills the table with the content in the database
                    while ($row = $result->fetch_assoc()) {
                        
                       echo '<tr><td>' . $row["name"] . '</td><td>' . $row["email"] . '</td><td>' . $row["subject"] . '</td><td>' . $row["message"] . '</td><td><button id="replied" onclick="DeleteRow()"></button>' . '</td></tr>';
                     
                    }

                    echo "</table";
                } else {
                    echo "0 result";
                }

                $conn->close();
                ?>

            </table>

And this is the javascript code to delete the table row:

    <script>
                function DeleteRow() {
                      
                      var td = event.target.parentNode; 
                     var tr = td.parentNode; // the row to be removed
                    tr.parentNode.removeChild(tr);
                }
    </script>

Picture of my table row The blank space underneath the "Have you replied" table header is where the button should be but its not there

I am still learning PHP so any replies on how I can fix this would be very helpful please

0 Answers
Related