I have a .json file with data from Wi-Fi thermometer and I need to upload this data to website using PHP.
Then using JavaScript I need to rewrite the data on the website each few seconds with a new one from the same file.
I have following code:
<script type="text/javascript">
function Load()
{
<?php
$json = file_get_contents('data.json');
$data = json_decode($json, true);
?>
var h = "<?php echo("Humidity: " . $data['humidity'] . "%");?>";
var t = "<?php echo("Tempature: " . $data['tempature'] . "°C");?>";
document.getElementById("hum").innerHTML = h;
document.getElementById("temp").innerHTML = t;
}
setInterval(Load, 10000);
</script>
<div>
<p id="hum"></p>
<p id="temp"></p>
</div>
Although there is setInterval(jsonDataLoad, 10000)
, it loads the data from the .json file and writes them to the website, but only once when the webpage loads.
So, am I doing something wrong or is the some restriction in PHP or JavaScript, thats causing the code to not work properly?