I have done a lot of researching in the last hours only finding results for windows servers.
My problem is that I got a c++ program on a embedded computer which needs to be executed when the user wishes to. That's why I set up a webserver to handle that. So I want to create a html page (already done) with a button called "Start Program", which then executes my c++ program.
I already tried Javascript child_process
which somehow didn't work and also php system()
call, which was unhandy because it didn't react to the button click alone, but to refreshing of the page too.
Is there a smart way to do that? Thanks!
If you don't want to refresh the page, you can program the button to make an ajax call, using jquery. You can add the following the your html index page:
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.1.js"></script>
<script>
<!--
$(document).ready(function() {
$('#buttonid').on('click', function(e) {
e.preventDefault(); // prevent page from reloading.
$.get('/callmyprogram.php');
});
});
-->
</script>
Also on your html, the ID of the button must match the selector on the JS code above. The selector #buttonid
implies that the ID of your button is buttonid
. So, the html for the button would be something like:
<input type="submit" id="buttonid" value="Run C++ program"/>
And then you would have callmyprogram.php
call your c++ program:
<?php
exec("/usr/local/bin/myprogram", $output, $ret);
echo "output: $output" . PHP_EOL;
echo "ret: $ret" . PHP_EOL;