0

I have developed c++ program which I execute this way on terminal

./dtapi get_probability dc simulated_diabetes_incidence_data_new.txt  AGE 70 weight 34 height 5.5 sex 0 ds1 34

And the result looks like

{"risk_of_disease":"2.122e-314"}

Now I want to take input parameter from Front end, send it to executable.

What is the possible way to do this using Javascript Jquery, so that response can be received directly. ?

Edit

I will take user input from browser, when use press button, I will read input parameter using js/jquery and send it to back end c++ executable

11
  • you want to do this from a browser via a server? Commented Nov 21, 2016 at 4:44
  • I will take user input from browser, when user press button, I will read input parameter using js/jquery and send it to back end c++ executable @JaromandaX Commented Nov 21, 2016 at 4:45
  • you need jquery.ajax - I take it you've made the appropriate configuration to your server to execute the dtapi command when appropriate? Commented Nov 21, 2016 at 4:47
  • also realize that there could be big security implications if you are allowing arbitrary input from users which outputs to a terminal command. For instance a user could possibly craft an input value that executes additional commands on your server which you are not intending. This kind of thing is hard to get right from a security standpoint. Commented Nov 21, 2016 at 4:49
  • @Aliester - as far as I can make out he's only asking about frontend, so lets hope he has the back end sorted out and functioning correctly and securely Commented Nov 21, 2016 at 4:51

1 Answer 1

1

It is very simple actually. Assume we have an adder program made in C++ that add the numbers passed as arguments like this:

./adder 2 3 6
{"risk_of_disease":"11"}

If we use a server side language like PHP, then we can have a simple form like this:

<form method="post">
    <input type="text" name="num1" value="<?php echo $num1; ?>">
    <input type="text" name="num2" value="<?php echo $num2; ?>">
    <input type="text" name="num3" value="<?php echo $num3; ?>">
    <button>Submit and run executable adder</button>
</form>

And the PHP code like this:

if(isset($_POST['num1']) &&
   isset($_POST['num2']) &&
   isset($_POST['num3']))
{
    $num1 = $_POST['num1'];
    $num2 = $_POST['num2'];
    $num3 = $_POST['num3'];
    $exec_cmd = "../bin/adder {$num1} {$num2} {$num3}";
    $json_result = shell_exec($exec_cmd);
} else {
    $num1 = 0;
    $num2 = 0;
    $num3 = 0;
}
  • The location of the executable is on '../bin/' directory.
  • The executable must have same ownership as the PHP user executing the script page (ex.: chown web35:client3 ./adder).
  • PHP should not have shell_exec in the disable_functions for the specific pool/user, or else it won't be able the run the program.

A complete example:

<?php 

if(isset($_POST['num1']) &&
   isset($_POST['num2']) &&
   isset($_POST['num3']))
{
    $num1 = $_POST['num1'];
    $num2 = $_POST['num2'];
    $num3 = $_POST['num3'];
    $exec_cmd = "../bin/adder {$num1} {$num2} {$num3}";
    $json_result = shell_exec($exec_cmd);
} else {
    $num1 = 0;
    $num2 = 0;
    $num3 = 0;
}

?>

<form method="post">
    <input type="text" name="num1" value="<?php echo $num1; ?>" size="5">
    <input type="text" name="num2" value="<?php echo $num2; ?>" size="5">
    <input type="text" name="num3" value="<?php echo $num3; ?>" size="5">
    <button>Submit and run adder</button>
</form>

<?php if(isset($json_result)): ?>
<h3>JSON result from executable</h3>
<pre><?php echo $json_result; ?></pre>
<?php endif; ?>

RESULT

enter image description here

EDIT

For a jQuery ajax call, just create a php file with the following code:

exerun.php

if(isset($_POST['num1']) &&
   isset($_POST['num2']) &&
   isset($_POST['num3']))
{
    $num1 = $_POST['num1'];
    $num2 = $_POST['num2'];
    $num3 = $_POST['num3'];
    $exec_cmd = "../bin/adder {$num1} {$num2} {$num3}";
    die(shell_exec($exec_cmd));
} else {
    die('{"risk_of_disease":"0"}');
}

And then have your simplejQuery ajax call liek this:

<form method="post" action="exerun.php">
    <input type="text" name="num1" value="0" size="5">
    <input type="text" name="num2" value="0" size="5">
    <input type="text" name="num3" value="0" size="5">
    <button type="submit">Submit and run adder</button>
</form>

<h3>JSON result from executable</h3>
<textarea id="result" cols="30"></textarea>


<script type="text/javascript">

jQuery(function($) {

    $('form').on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: $(this).attr('action'),
            data: $(this).serialize(),
            success: function(response) {
                $('#result').text(response);
            }
        });
    });
});

</script>

UPDATE

If you want to run an AJAX call without a submit form, then you can pass the arguments directly as an object and have the url manually assigned like this:

<button id="execall">Run AXAJ remote exec</button>

<h3>JSON result from executable</h3>
<textarea id="result" cols="30"></textarea>


<script type="text/javascript">

jQuery(function($) {
    $('#execall').on('click', function(e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: 'exerun.php',
            data: {
                'num1': 10,
                'num2': 20,
                'num3': 30
            },
            success: function(response) {
                $('#result').text(response);
            }
        });
    });
});

</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Apart from using form submit methods, can you pleas suggest alternate method where I can call the php script during ajax call only. Without using form
You can make a direct AJAX call to the PHP exec script. See my updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.