首页 > 解决方案 > 为什么我的 javascript 代码刷新但 PHP 不刷新?

问题描述

所以,我有一些 javascript 代码有人给了我刷新页面的一部分,它通过刷新分配给它的随机数的变量来工作。但是当我尝试刷新其中的 php 代码时,它只会运行一次。为什么是这样?有没有办法可以解决这个问题?

//other working php code

if(isset($_POST['hostgame'])) 
    {

//do some stuff
print"<div id='target'> </div>";

    }

<script >

setInterval(function refresh(){
      // why isn't this refreshing? math.random is?

    <?php
      $query = "select * from gameStatus".$_SESSION['gamenumber'];
      $result = mysqli_query($link, $query);

        while($row = mysqli_fetch_row($result))
        {            
            list($id, $status) = $row;
        }
      $gameStatus=$status;
    ?>      

  var value = "<?php echo $gameStatus; ?>"+Math.round(Math.random()*100);

  $('#target').addClass('hidden');
  $('#target').html(value);
  $('#target').removeClass('hidden');} , 1500);

  </script> 
</body>
</html>

所以游戏状态第一次被打印出来,但不会刷新以显示它在 mysql 中的最新更改。我检查了数据库,它正在更改,但没有显示在屏幕上。随机数虽然令人耳目一新。有人可以解释为什么吗?也许如何解决它?

标签: javascriptphp

解决方案


推荐阅读