Stonesmit Stonesmit - 5 months ago 32
PHP Question

Jquery hide row from table on button press

I have the following table code:

echo "<table border='1'>
<tr>
<th>Code</th>
<th>Name</th>
<th>Price</th>
</tr>";

$sql = "SELECT * FROM products where updated_account_ID='$aktiv'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['code'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . "<button name='code' value='".$row['code'] ."'>Delete</button></td>";

echo "</tr>";


I would like to use jquery to hide or delete the specified row after pressing the "Delete" button which also deletes the record from the database. Could someone be so kind and help be do it?
Thank you!

Answer

jQUERY :

$(".delete").click(function(){
var product_id = $(this).parents('tr').attr('data-product-id');
$(this).parents('tr').remove();
$.ajax({
    url: "remove-record.php",
    data: product_id,
    type: "post",
    success: function(result){
        alert(result);
    }
});

});

PHP FILE :

echo "<table border='1'>
    <tr>
        <th>Code</th>
        <th>Name</th>
        <th>Price</th>
    </tr>";

$sql = "SELECT * FROM products where updated_account_ID='$aktiv'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<tr data-product-id='".$row['id']."'>";
echo "<td>" . $row['code'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . "<button name='code' value='".$row['code'] ."'>Delete</button
</td>";

echo "</tr>";

remove-record.php :

$product_id = $_POST['data'];
$sql = "DELETE FROM products WHERE id=product_id";
echo json_encode('Record deleted');