【问题标题】:Manually refresh page from another page, on the same server从同一服务器上的另一个页面手动刷新页面
【发布时间】:2015-06-19 08:59:36
【问题描述】:

我正在尝试使用代码从 PHP 文件(假设为 A)中将 MySQL 标志设置为 true。我想从另一个已经打开的 PHP 文件 (B) 中读取这个 MySQL 标志。问题是:从第一页(A)更改标志值后,有没有办法手动刷新此(B)页?

可能正在使用 cron 或类似的东西,我真的不想每 X 秒刷新一次页面 B,直到它读取新的标志值。

【问题讨论】:

标签: javascript php mysql cron


【解决方案1】:

使用 AJAX。

在B页面内部创建一个请求函数来检查MySQL标志的状态。如果标志设置为true,请刷新页面。

在B程序的HTML中插入:

<head>
...
  <script>
    function enableChecker() {
        setInterval( checkFlag, 10000); // Check each ten seconds
    }

    function checkFlag() {
        xmlhttp = GetXmlHttpObject();
        if ( xmlhttp==null ) return;
        xmlhttp.onreadystatechange = function() {
            if ( xmlhttp.readyState == 4 ) {
                if ( xmlhttp.responseText == "OK" ) {
                    location.reload(); // Refresh the page
                }
            }
        }
        xmlhttp.open( 'GET', 'myCheckProgram.php', true ); // Call php program to check the flag value
        xmlhttp.send( null );
        return false;
    }
  </script>
</head>
<body onload="enableChecker()" >

创建一个名为 myCheckProgram.php 的程序

<?php

/* Blah blah to connect with database and query for flag */

$flag = // Result of query

echo $flag ? 'OK' : 'NOK' // Return OK if flag is true

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 2015-04-17
    • 1970-01-01
    相关资源
    最近更新 更多