Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh table with jquery ajax?

Tags:

jquery

ajax

I have the below table. With ajax call I am updating the content of the table. It is reflected in the database, but how can I refresh the table without reloading the entire page. Please someone guide me.

My table structure:

<table class="table table-hover" id="cust-table">
    <thead>
        <tr>
            <th>LastName</th>
            <th>FirstName</th>
        </tr>
    </thead>
    <tbody>
        <?php
            for($i=0; $i<$numrows; ++$i) {
                 $contacts = mysqli_fetch_assoc($result);
            ?>
        <tr>
            <td><?php echo $contacts['LastName']; ?></td>
            <td><?php echo $contacts['FirstName']; ?></td>
        </tr>
        <?php
        } ?>
    </tbody>
</table>

Jquery code:

$.ajax({   
       type: 'POST',   
       url: 'update.php',   
       data: {LastName:last_name, FirstName:first_name}
     });

the update.php is updating the database but I need to refresh the table without refreshing the entire page. Can someone please help me on this?

like image 568
user3652549 Avatar asked Sep 30 '22 04:09

user3652549


1 Answers

This should be easy.

    //delete all rows in table
    $('#cust-table').empty();

    //add rows
    $('#cust-table > tbody:last').append('<tr> ... </tr>'); //do the magic
like image 135
Dirk Avatar answered Oct 07 '22 19:10

Dirk