简体   繁体   English

无需刷新即可更新 PHP 变量

[英]Update PHP variable without refresh

I am new in JSON and PHP programming.我是 JSON 和 PHP 编程的新手。 I am making a web page that allows to view data from file.py , those data will be linked in a gauge and updated second by second .我正在制作一个网页,允许查看file.py中的数据,这些数据将在gauge链接并second by second更新。

  var gauge1;
  var x = <?php echo python /home/usr/Desktop/file.py ?> ;

    setInterval(function() {
      gauge1.refresh(x);
    }, 1000);
  };

I can make the gauge shows just the first reading from "file.py" but is stuck there, the only way to update the gauge is refreshing the page and the new reading is shown in the gauge.我可以让仪表只显示“file.py”中的第一个读数,但卡在那里,更新仪表的唯一方法是刷新页面,新读数显示在仪表中。

I think the problem is in:我认为问题在于:

gauge1.refresh(x);仪表1.刷新(x);

because when I write:因为当我写:

gauge1.refresh(getRandomInt(0,50)); Gauge1.refresh(getRandomInt(0,50));

The gauge always show random data, updating a new random data between 0 and 50 .仪表始终显示随机数据,更新random data between 0 and 50的新random data between 0 and 50

Is there any solution to make the gauge always shows automatically data from file.py without refresh the page?是否有任何解决方案可以使仪表始终自动显示来自file.py 的数据而不刷新页面?

You can use ajax to execute a backend script in a time interval like this .您可以使用 ajax 在这样的时间间隔内执行后端脚本。

var x=null;
setInterval(retriveData, 300000); 
function retriveData() {
   $.ajax({url: "pyData.php", success: function(data){
      x=data;
   }});
}

And your Php code like this (pyData.php)你的 PHP 代码是这样的(pyData.php)

<?php 
 $command = escapeshellcmd('/home/usr/Desktop/file.py');
 $output = shell_exec($command);
 echo $output;
 ?>

You can execute like this你可以这样执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM