Christian Wibowo Christian Wibowo - 5 months ago 28
Ajax Question

Auto refresh table didn't work

I want to refresh my table every 5 seconds..

Now I'm using this code:

<script type="text/javascript">
$(document).ready(function(){
refreshTable();
});

function refreshTable(){
$('#content').load('refresh_data_dapur.php');
setTimeout(refreshTable, 5000);
}
</script>


refresh_data_dapur.php

$sql = "select t.no_meja, d.nama_menu,d.jumlah,t.status from detail_transaksi d join transaksi t on(d.id_transaksi=t.id_transaksi) where t.status ='pending' and d.status_menu = 'pending'";
$result = mysql_query($sql);
echo '<table cellpadding="5" cellspacing="0" border="1">';
echo '<tr>';
echo '<th>No Meja</th>';
echo '<th>Nama Menu</th>';
echo '<th>Jumlah</th>';
echo '<th>Status</th>';
echo '</tr>';
while($detail_transaksi = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td>';
echo $detail_transaksi['no_meja'];
echo'</td>';
echo '<td>';
echo $detail_transaksi['nama_menu'];
echo '</td>';
echo '<td>';
echo $detail_transaksi['jumlah'];
echo '</td>';
echo '<td>';
echo $detail_transaksi['status'];
echo '</td>';
echo '</tr>';
}


in my html :

<div id="content">

</div>


When I try to load the page, it doesn't show anything...

When in the html I modify into this code, it shows the data but the table still isn't refreshing...

<div id="content">
<?php include("refresh_data_dapur.php");?>
</div>


How can I fix this?

Answer Source

Everything is fine here. but you are using setTimeout(refreshTable, 5000);. It will run the refreshTable function once after 5 seconds of page load. You should be using

setInterval(refreshTable, 5000);

instead. That will run every five second and keep it outside and after refreshTable function because you are using it in setTimeout before it is declared. That might be the cause.