简体   繁体   English

php无法调用shell脚本(js-> php-> sh)

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

I want to execute a shell script from webpage. 我想从网页执行Shell脚本。 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 . 如果用户单击提交按钮,则javascipt会进行验证,然后通过ajax调用php文件。 Php file extracts the arguments and call sum.sh with those arguments. PHP文件提取参数,并使用这些参数调用sum.sh
But i'm unable to do it. 但是我做不到。 Here is my code. 这是我的代码。

design3.html : 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 : 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 : sum.sh

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

I'm new to this JS and PHP. 我是这个JS和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. 当我在没有JS的同一个文件中编写html和php时,我的脚本正在工作。 If I'm not calling sum.sh script file in php, it is working. 如果我没有在php中调用sum.sh脚本文件,则它正在工作。 Coming to total integration it is failing. 全面集成失败了。 I'm getting this error from browser alert Request failed: error . 我从浏览器警报中收到此错误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. 我认为问题出在javascript或php中。 But the problem is in my html code. 但是问题出在我的html代码中。 One of my friend solved this problem. 我的一位朋友解决了这个问题。
He told I took submit button. 他告诉我点击了提交按钮。 So he changed the type of button submit to button . 因此,他将按钮的类型更改为submitbutton 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()"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM