javascript - 为什么我的 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 中的最新更改。我检查了数据库,它正在更改,但没有显示在屏幕上。随机数虽然令人耳目一新。有人可以解释为什么吗?也许如何解决它?
解决方案
推荐阅读
- php - cronjob 执行一个包含大约 100 个 curl url 的脚本 - 不好的做法?
- java - Java Mail API - 无法正确设置端口
- nginx - 字体文件被 CORS 策略阻止
- c++ - OpenCL 在 GPU 上分配结构会导致垃圾
- java -
没有有效的 android:name - .htaccess - 合并多个 FilesMatch(特定文件)
- java - FormLayout:2 个字段的 1 个标题
- express - axios 循环承诺并更新之前的 axios 响应数据
- php - 带有 SolrClient 的 PHP Slim
- r - `bookdown`/`rmarkdown`/`knitr`: 输出文档完成后执行挂钩?