Pages

Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Sunday, 17 June 2018

Call php script from javascript

The idea is to create web interface for raspberry pi. I want to reload only certain elements of the http page and have possibility to execute linux commands. For example if I want to check CPU temperature I use vcgencmd measure_temp. I put this into php script and print complete javascript.

Below is my code: 

<?php
$result = exec ('sudo vcgencmd measure_temp'); 
$tempOnly = substr($result,5);  
?>

function rpiTemp()
{
        var example = <?php echo '"' . $tempOnly . '"'; ?>;
        alert(example);

}



In <head> block I include script with following:

<script type="text/javascript" src="http://raspberrypi/check_cpu_temp.php"></script>

And call it in <body> section with a button:

<input type="submit" value="Check CPU Temperature" onclick="rpiTemp()"/>