1

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!

Peter Smith
  • 5,528
  • 8
  • 51
  • 77
rele92
  • 79
  • 11
  • This is way broad. Almost certainly, however, will your server language have some `exec` ability or similar. – erip Mar 09 '16 at 12:50
  • could you provide more info about environment (linux, windows..etc) and show what you already did? – profesor79 Mar 09 '16 at 12:50
  • if you run php on your server, look at here: http://stackoverflow.com/questions/4279003/execute-c-program-with-php-script () – Irrmich Mar 09 '16 at 12:51
  • It is certainly possible, if I was in your place I would use the php system, maybe connected with some ajax stuff to avoid reloading the full page. – Marco Mar 09 '16 at 12:53
  • Instead of PHP you can also use plain, old CGI. – pkalinow Mar 09 '16 at 12:55
  • im working on a debian system. i tried to use
    and then placing a but somehow i get an internal server error when i try to run that.
    – rele92 Mar 09 '16 at 13:01

1 Answers1

6

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;
Diogo Melo
  • 1,735
  • 3
  • 20
  • 29
  • sounds like that is what i need, but as im relatively new to web programming, where do i need to put that &.on'click function? is it a – rele92 Mar 09 '16 at 13:01
  • I have edited the answare to better explain where to place the js code. – Diogo Melo Mar 09 '16 at 13:32
  • thanks, but i get a syntax error at &.('#buttonid').on . do i need to fill in my name of my input type anywhere? – rele92 Mar 09 '16 at 13:43
  • It must mean something wrong on the PHP side. What happens when you try to access the /callmyprogram.php directaly, like "curl http://localhost:8888/callmyprogram.php"? – Diogo Melo Mar 09 '16 at 13:47
  • i just edited my comment, the internal server error was my fault. but i dont get where i have to put the name. – rele92 Mar 09 '16 at 13:49
  • I have edited the answer. By name you mean the "buttonid" selector? It must match the id of the input. – Diogo Melo Mar 09 '16 at 13:55
  • thanks, i understand that. nonetheless i get SyntaxError: missing name after . operator in line &.('#buttonid').... at the . after & – rele92 Mar 09 '16 at 14:07
  • small mistake. remove the dot between "$" and "('buttonid')". I have edited the answer. – Diogo Melo Mar 09 '16 at 14:10