0

I have a problem with refreshing PHP variable... PHP variable after refreshing is still same - not refreshed

My little JS:

var reload = function() {
    var pred = <?php Echo json_encode(reload()); ?>;

    $(".reload").fadeIn("fast").text(pred);
    setTimeout(reload, 3000);
}
setTimeout(reload, 3000);

My PHP function:

function reload() {
        ForEach ($servers as $server) {
            $s = Explode(":", $server);
            $Data = $status->getStatus($s[0], $s[1]);
            If (!$Data) {} Else { $c1 = $c1 + $Data['Players']; } 
        }
        Return $c1; //Players returned 
}

If I joined the game after during this script the variable is the same as on beggining... Nothing was changed. Why my variable isn't refreshing ? What am I doing wrong?

Thanks for all help and sorry for my bad eng

4

2 回答 2

3

reload()只调用一次:当脚本第一次执行时。在将任何输出(包括 HTML 和 JavaScript)发送到浏览器之前,所有 PHP 处理都在服务器端完成。

当您的 JavaScript 函数执行时,它会重新使用reload()脚本第一次运行时生成的值。

如果您想通过 PHP 生成新值,则需要刷新页面或创建 AJAX 调用以从服务器获取新数据。

于 2013-08-09T20:11:54.150 回答
0

就像是

var reload = function() {
    $.getJSON('reload.php', function(data){
         $(".reload").fadeIn("fast").text(data);
         setTimeout(reload, 3000);
    });
}

reload();

在你的 reload.php 文件中

function reload() {
        ForEach ($servers as $server) {
            $s = Explode(":", $server);
            $Data = $status->getStatus($s[0], $s[1]);
            If (!$Data) {} Else { $c1 = $c1 + $Data['Players']; } 
        }
        Return $c1; //Players returned 
}

echo json_encode(reload());
于 2013-08-09T20:15:21.540 回答