简体   繁体   中英

Update value on web page dynamically

I have a microcontroller which reads a thermocouple and sends its values to a textfile on the Raspberry Pi.

On the Pi runs a apache server which hosts my website. The website shows the value from the textfile, but to get the actual value I have to refresh the page.

index.php

 <html> <?php $temp = file_get_contents('Temp.ESP'); ?> <header> <h2><?php echo $temp; ?> °C</h2> </header> </html> 

Thanks in advance

with javascript Use the setTimeout() global method to create a timer. This will refresh the content after 5000 milliseconds (5 seconds): also don't forget to load the jquery lybrary inside the html head adding

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>
setTimeout(function(){ 

$.get( "mydata.php", function( data ) {
$( "#mydata" ).html( data ); // this will replace the html refreshing its content using ajax

});


 }, 5000);` 
</script>

on the html change

<header>
<h2 id="mydata"></h2> ºC
</header>

notice the id="mydata"

the php only needs to echo the file contents also create the file mydata.php

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