简体   繁体   中英

php is failing to call shell script (js -> php -> sh)

I want to execute a shell script from webpage. So I started with a simple arithmatic sum program. Webpage has two fields and a submit button. If user clicks submit button, javascipt validates and then calls php file through ajax . Php file extracts the arguments and call sum.sh with those arguments.
But i'm unable to do it. Here is my code.

design3.html :

 <body>
 <form action='' method='post'>
        <center>
           arg1: <input type='text' name='t1' id="arg1"/> <br><br>
           arg2: <input type='text' name='t2' id="arg2"/> <br><br>
           <input type='submit' name='use_button' value='Sum' onclick="addContent()"/>
        </center>
    </form>

 <script type="text/javascript">
    function addContent(){
        var a = document.getElementById('arg1').value;
        var b = document.getElementById('arg2').value;

        if(a == "" || b == "") {
            alert("Please fill all fields");
            return;
        }

        var request = $.ajax({
                   url: "add.php",
                   type: "POST",
                   dataType: "html",
                   data: {functionname: 'add', arguments:[a, b]}
         });

         request.done(function(msg) {
                   alert(msg);
          });

          request.fail(function(jqXHR, textStatus) {
                   alert( "Request failed: " + textStatus );
          });

    }

</script>
</body>

add.php :

<?php
if (is_ajax()) {
    if( isset($_POST['functionname']) ) {
        switch($_POST['functionname']) {
            case 'add':
                $var1 = $_POST['arguments'][0];
                $var2 = $_POST['arguments'][1];
                $cmd = 'sh ./sum.sh'.' '.$var1.' '.$var2;
                $output = shell_exec($cmd);
                echo $output;
                break;
         }
     }
}

function is_ajax() {
    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
?>

sum.sh :

#!/bin/bash
var1=$1
var2=$2
echo `expr $var1 + $var2`

I'm new to this JS and PHP. But tried hard to do this. If I'm testing seperately it's working. When I write html and php in same file without JS then my script is working. If I'm not calling sum.sh script file in php, it is working. Coming to total integration it is failing. I'm getting this error from browser alert Request failed: error .

You POST to the server

functionname: 'add'

It does not match with your server-side test

switch($_POST['functionname']) {
    case 'connect':

I thought the problem is in javascript or php. But the problem is in my html code. One of my friend solved this problem.
He told I took submit button. So he changed the type of button submit to button . Now it's working fine.

Change this code:

<input type='submit' name='use_button' value='Sum' onclick="addContent()"/>

to:

<input type='button' name='use_button' value='Sum' onclick="addContent()"/>

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