如何使用Javascript进行PHP更新?

我有个php文件,其中包含更新数据库的方法。但是,我想知道在Java语言中如何做到这一点?即每5秒钟“访问”此页面,以便更新其内容。

这是我的update.php文件:

<?php include('config.php') ?>

<?php
mysql_query("UPDATE paint SET paint_points='test'") or die(mysql_error());
echo "Updated";
?>

谢谢!

Use the setInterval function with an (a)jax request every 5 secs in javascript:

//syncronized jax:
function myjax() {
    var oXhr = new XMLHttpRequest();
    oXhr.open("POST", "yourphp.php", false);
    oXhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
    oXhr.send(null);
}

//set an interval each 5 seconds to call your myjax method
setInterval(function() { myjax(); }, 5000);

In this example the request is synchronous but it could be asynchronous if you wished so.

The simplest case is to reload the page with:

<script type="text/javascript">
 setInterval(function() { location.reload(true); }, 5000);
</script>

You can get fancier if you use an ajax call to fetch the page.

Using jQuery:

(function() {
    var updateAgain = arguments.callee;
    $.get('/url/to/script.php', function() {
        setTimeout(updateAgain, 5000);
    });
})();

The advantage of this over setInterval is that it won't start counting to five seconds until the request is finished; this is important if the request takes more than a second or two. It will also stop if a request fails (which may or may not be an advantage).