0

I want to remove DB row data from HTML table. I have an HTML table where I have called all the database table values. I want to give a delete order option to the user. I have used a delete button for that. To do this, I am trying to use ajax and jquery with php delete query. But the issue is When I click on delete it just deletes the HTML row data. It should delete that particular id row from DB as well. I need your help. Please let me know what I have done wrong? I am very much new to ajax and jquery.

remove.php

<?php 
include "config.php";

$id = $_REQUEST['id'];

// Delete record
$query = "DELETE FROM mi_home_tv WHERE id='$id'";
mysqli_query($link,$query);

echo 1;

jquery+ajax code
<script src='jquery-3.0.0.js' type='text/javascript'></script>
<script type="text/javascript">
    $(document).ready(function(){

    // Delete 
    $('.delete').click(function(){
        var el = this;
        var id = this.id;
        var splitid = id.split("_");

        // Delete id
        var deleteid = splitid[1];

        // AJAX Request
        $.ajax({
            url: 'remove.php',
            type: 'POST',
            data: { id:deleteid },
            success: function(response){

                // Removing row from HTML Table
                $(el).closest('tr').css('background','tomato');
                $(el).closest('tr').fadeOut(800, function(){      
                    $(this).remove();
                });
            }
        });
    });
});
</script>

Button Code:

echo "<td> <span  id='del_<?php echo $id; ?>'  type='submit' class=' delete btn c-theme-btn c-btn-uppercase btn-xs  c-btn-square c-font-sm'>Delete</span> </td>";

php code:

 <form method="post" action="remove.php">
                                        <?php
                                       echo " <thead>
                                            <tr>

                                                <th>Order ID</th>
                                                <th>Date</th>
                                                <th>Name</th>
                                                 <th>Store Name</th>
                                                <th>Zip</th>
                                                <th>City</th>
                                                <th>Address</th>
                                                 <th>Contact</th>
                                                  <th>Tv Varient</th>
                                                  <th>Total</th>
                                                   <th>Delivery</th>
                                                  <th>Action</th>
                                            </tr>
                                        </thead>

                                              <tbody>";
                                               while($row = mysqli_fetch_array($result))
                                          {
                                             $id = $row['id'];

                                           echo"<tr>";
                                                echo "<td>" . $row['id'] . "</td>";
                                                echo "<td>" . $row['rdate'] . "</td>";
                                                echo "<td>" . $row['rname'] . "</td>";
                                                echo "<td>" . $row['rstore'] . "</td>";
                                                echo " <td>" . $row['rzip'] . "</td>";
                                                echo "<td>" . $row['rcity'] . "</td>";
                                                echo "<td>" . $row['raddress'] . "</td>";
                                                echo "<td>" . $row['rphone'] . "</td>";
                                                echo "<td>" . $row['rtv_varient'] . "</td>";
                                                echo"<td>" . $row['ramount'] . "</td>";
                                                echo"<td>" . $row['rdelivery'] . "</td>";

                                                echo "<td> <input type='submit' value='delete'  id='del_<?php echo $id; ?>' type='submit' class=' delete btn c-theme-btn c-btn-uppercase btn-xs  c-btn-square c-font-sm'>Delete</button> </td>";

                                                  echo"</tr>";


                                                 }
                                                 ?>
                                             </form>
1
  • Without seeing your PHP code we cannot begin to guess the problem. Are there any errors in the console? Commented Feb 18, 2018 at 3:10

1 Answer 1

0

You should have a form which will have an input as follows:

<form method="post" action="remove.php">
<input type="submit" value="Delete" name="id" />
</form>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.