5

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?

1
  • 3
    I've voted to close this as too broad. You'd benefit from looking through jQuery's documentation (especially that of its AJAX method: api.jquery.com/jquery.ajax). Commented Dec 17, 2014 at 12:09

2 Answers 2

2

This should be easy.

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

    //add rows
    $('#cust-table > tbody:last').append('<tr> ... </tr>'); //do the magic
Sign up to request clarification or add additional context in comments.

Comments

2

For your scenario, write code for html table and ajax in other pages. For example:-

In update.php

//do stuff for update
//html table code
------------------------

In ajax page(test.php)

<div id='load'>

</div>

<script>
$(function(){
loadData();

// call by click event
$('#selector-name').click(function(){
     loadData();
});


function loadData(){
    $.ajax({   
     type: 'POST',   
     url: 'update.php',   
     data: {LastName:last_name, FirstName:first_name},
    success: function(msg) {
            $("#load").html(msg);
        },
    });
  }
});
</script>

2 Comments

Thanks for the reply. I tried what you suggested. The page is not refreshing now but the table disappears. It appears only when I reload the page again. How can I refresh the table alone. I am new to jquery.
put ajax code in function and call function when document is ready. check again post

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.