开发者

How to trigger a refreshing (reloading) of .php page when a new database entry is added?

I have an insertUpdate.php which insert table rows into MYSQL database,

<?php $userid=$_POST["userid"]; $timestamp=$_POST["timestamp"]; $addALine="INSERT INTO myDatabase (userid,timestamp) VALUES ('$userid','$timestamp')";
$intoDatabase=mysql_query($addALine); ?>

and a showUpdate.php which show the table as a web page,

while ($latestRow=mysql_fetch_row($result) )
{ $data .= "<tr><td>".$latestRow[0]." </td><td> ".$latestRow[1]."</td></tr>\n";
}

I do not want to set an automatic refreshing every several seconds like <?php header('Refresh: 8'); ?>.

How t开发者_如何学Pythono trigger a refreshing/reloading of the showUpdate.php when there is a new entry into the database inserted by the insertUpdate.php? Thanks for any help!


Web pages are stateless. Once you send the response, it is all over.

If you want a web page to find out about a new record, it will have to poll the server to ask if there are any new records. You cannot send a notification from the server to a served-page.


You could poll the database using AJAX(in the background) and refresh the page when a new entry is added.

Because of http being a stateless protocol there is no other option besides refreshing or polling.


Use a XMLHttpRequest to keep page state.

setInterval(function(){ // every 10 seconds
    // using jQuery
    $('#container').load('/showUpdate.php'); // load php page into #container
}, 10000);

There are no events in a web page for when the database has changed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜