Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh a table with jQuery/Ajax every 5 seconds

Tags:

So I have a table pulling information from a database and I was wondering how I could make it refresh its information without reloading the whole page.

like image 323
Ben Cobbold Avatar asked Apr 15 '11 19:04

Ben Cobbold


People also ask

How do you call AJAX every 5 seconds?

Creating a function that calls the AJAX request and using this function in setInterval() and set Interval for 5 sec. Now the function executes every 5 seconds and fetches new data from the server. It repeatedly executes the function even when the previous AJAX request is not successfully executed and return.

How do you refresh a table in JavaScript?

A table displays data, usually from a query - if you want to reload the query you can . trigger() it from any script, or another component (like a custom button) or the refresh button option in the table component will do this for you.

Does AJAX refresh the page?

AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.


1 Answers

You'll need a getTable.php page that displays your table, and nothing else: no headers, footers, etc.

PHP (getTable.php) - this can be any server side code (asp, html, etc..)

<?php     echo '<table><tr><td>TEST</td></tr></table>'; ?> 

Then, in your JS, you can easily refresh the table by using the load() method:

HTML

<div id="tableHolder"></div> 

JS

<script type="text/javascript">     $(document).ready(function(){       refreshTable();     });      function refreshTable(){         $('#tableHolder').load('getTable.php', function(){            setTimeout(refreshTable, 5000);         });     } </script> 
like image 112
Dutchie432 Avatar answered Sep 19 '22 18:09

Dutchie432