简体   繁体   中英

Update PHP variable without refresh

I am new in JSON and PHP programming. 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 .

  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.

I think the problem is in:

gauge1.refresh(x);

because when I write:

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

The gauge always show random data, updating a new 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?

You can use ajax to execute a backend script in a time interval like this .

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 
 $command = escapeshellcmd('/home/usr/Desktop/file.py');
 $output = shell_exec($command);
 echo $output;
 ?>

You can execute like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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