AJAX Delete row in table with PHP id selected

LatentDenis

How would you delete a row from a table using AJAX based on this code?

Here's the PHP code I'm working with:

foreach ($results as $result) {
    echo "<tr><td>".$result['first_name']."</td><td>".$result['last_name']."</td><td><button class=\"btn btn-sm btn-danger delete_class\" id=\"".$result['id']."\" >DELETE</button></td></tr>";
}

As you can see, the button has an id paired with it.

Here's my jquery/AJAX code to delete the file from the database:

<script>
        var $tr = $(this).attr('parentElement');

        $('.delete_class').click(function(){
            var del_id= $('.delete_class').attr('id');
            $.ajax({
                url:"delete_page.php?delete_id="+del_id,
                cache:false,
                success:function(result){
                    $tr.find('td').fadeOut(1000,function(){
                        $tr.remove();
                    });
                }
            });
        });
</script>

There is also a PHP file that goes into the database and deletes the data, which works fine.

In the javascript code above ^ the variable being set at the top "$tr" is saying the 'parentAttribute' is the "td" not the "tr", how would I go up two parent attributes?

What can I change my "success:function(result){ }" to to make the row immediately disappear, because,

$tr.find('td').fadeOut(1000,function(){
      $tr.remove();
}

this code ^ isn't working.

RomanPerekhrest

Change your current jquery code as shown below:

<script>    
        $('.delete_class').click(function(){
            var tr = $(this).closest('tr'),
                del_id = $(this).attr('id');

            $.ajax({
                url: "delete_page.php?delete_id="+ del_id,
                cache: false,
                success:function(result){
                    tr.fadeOut(1000, function(){
                        $(this).remove();
                    });
                }
            });
        });
</script>

The closest method returns the first ancestor of the selected element. https://api.jquery.com/closest/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Updating AJAX request variable with ID of PHP generated table row on click

From Dev

Delete selected row from table in mysql

From Dev

Delete selected row from table in mysql

From Dev

delete row with ajax function and php

From Dev

jQuery delete table row by dynamic id

From Dev

How to delete a selected row in a html table by using jQuery?

From Dev

How to get id of selected table item in php

From Dev

How to get id of selected table item in php

From Dev

delete a specific row of a table using php

From Dev

PHP button delete row from table not working

From Dev

delete a specific row of a table using php

From Dev

Delete row from database with a button in table (PHP)

From Dev

PHP and AJAX - Append new data to a table, and new edit and delete buttons with the generated id

From Dev

delete row if checkbox is selected

From Dev

delete row if checkbox is selected

From Dev

How to change table row ID dynamically based on the row delete?

From Dev

How to change table row ID dynamically based on the row delete?

From Dev

Secure php -> Ajax delete item based on id

From Dev

PHP form delete from MYSQL table dont delete the right row

From Dev

Delete element from table by id with button php

From Dev

Update the value in table row by id - PHP

From Dev

Update the value in table row by id - PHP

From Dev

Highlight selected table row

From Dev

Add row when selected from one table to another using php

From Dev

php/pdo/mysql accessing selected row from a select * in html table

From Dev

Add row when selected from one table to another using php

From Dev

php/pdo/mysql accessing selected row from a select * in html table

From Dev

Delete selected record in PHP

From Dev

Qt Delete selected row in QTableView

Related Related

  1. 1

    Updating AJAX request variable with ID of PHP generated table row on click

  2. 2

    Delete selected row from table in mysql

  3. 3

    Delete selected row from table in mysql

  4. 4

    delete row with ajax function and php

  5. 5

    jQuery delete table row by dynamic id

  6. 6

    How to delete a selected row in a html table by using jQuery?

  7. 7

    How to get id of selected table item in php

  8. 8

    How to get id of selected table item in php

  9. 9

    delete a specific row of a table using php

  10. 10

    PHP button delete row from table not working

  11. 11

    delete a specific row of a table using php

  12. 12

    Delete row from database with a button in table (PHP)

  13. 13

    PHP and AJAX - Append new data to a table, and new edit and delete buttons with the generated id

  14. 14

    delete row if checkbox is selected

  15. 15

    delete row if checkbox is selected

  16. 16

    How to change table row ID dynamically based on the row delete?

  17. 17

    How to change table row ID dynamically based on the row delete?

  18. 18

    Secure php -> Ajax delete item based on id

  19. 19

    PHP form delete from MYSQL table dont delete the right row

  20. 20

    Delete element from table by id with button php

  21. 21

    Update the value in table row by id - PHP

  22. 22

    Update the value in table row by id - PHP

  23. 23

    Highlight selected table row

  24. 24

    Add row when selected from one table to another using php

  25. 25

    php/pdo/mysql accessing selected row from a select * in html table

  26. 26

    Add row when selected from one table to another using php

  27. 27

    php/pdo/mysql accessing selected row from a select * in html table

  28. 28

    Delete selected record in PHP

  29. 29

    Qt Delete selected row in QTableView

HotTag

Archive