如何使用ajax

时间:2017-08-15 01:40:19

标签: javascript php jquery html ajax

我正在尝试从远程服务器运行脚本并使用ajax将其输出到HTML页面,以便用户可以看到。我尝试使用up.sh和down.sh在我的本地服务器上执行此操作并且它可以正常工作。但是,当我将这两个脚本放在远程服务器上时,启动一个ssh会话并尝试从远程服务器获取输出,它没有显示任何内容,也没有给我任何错误。我错过了什么吗?另外你可以看到我硬编码了“网关”#39;到remoteserver,稍后我想从用户输入的form.php文本框字段中获取此值。最好的方法是什么?

form.php的:

    <div id="gatewayInput">
            <form>
                    <input type="text" id="gateway" name="gateway" placeholder="Gateway Name"><br><br>
            </form>
    </div>

<div class="box1">
  <form method="post">
  <label class="col">Up/Down</label>
  <span class="col">
    <input type="radio" name="option" id="r1" value="1" />
    <label for="r1">Up</label>
    <input type="radio" name="option" id="r2" value="2" />
    <label for="r2">Down</label>
  </span>
    <span class="col">
      <input type="submit" class="button"/>
    </span>
  </form>
</div>


<script src ="../../../jqueryDir/jquery-3.2.1.min.js"></script>
<script type="text/javascript">

        $(".button").click(function(event){
             if ((document.getElementsByName("gateway")[0].value == '')) {
                    alert('Gateway Required!');
                    return false;
             }
             else if (document.querySelectorAll('input[type="radio"]:checked').length < 1) {
                    //console.log("radio button length value: " + document.querySelectorAll('input[type="radio"]:checked').length);
                    alert('Please Choose Up/Down Value!');
                    return false;
             }
             else {
                    alert('Sucess!');
                    event.preventDefault();
                    $.ajax({
                            url:"testexe.php",
                            type: "POST",
                            data: {option: $('input[type=radio]:checked').val()},
                            dataType: "text",
                            success:function(result){
                            $('#div1').html(result)
                            }
                    });
                    return true;
            }
       });

testexe.php

<?php

include 'res/php/functions.php';
$gateway = 'remoteserver';
$user = 'user';
$pass = 'pass';

function cleanInput($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

//when the submit button is clicked
if ( $_SERVER['REQUEST_METHOD'] == 'POST'){
     //$gateway = cleanInput($_POST['gateway']);

      //create the ssh connection
    if ($connection = @ssh2_connect($gateway, 22)) {
           ssh2_auth_password($connection, $user, $pass);

           if(isset($_POST['option']) && $_POST['option'] == 1) { 
                $output = 
                 shell_exec("/tmp/testscripts/up.sh");
           }

           if(isset($_POST['option'])  && $_POST['option'] == 2) { 
                $output = 
                 shell_exec("/tmp/testscripts/down.sh");
           }
           //remove  this  if you want to see $output in this file

           echo "<pre>$output</pre>";
     }

} 
?>

1 个答案:

答案 0 :(得分:0)

我用以下内容修复了它:

if ( $_SERVER['REQUEST_METHOD'] == 'POST'){
     //$gateway = cleanInput($_POST['gateway']);

      //create the ssh connection
    if ($connection = @ssh2_connect($gateway, 22)) {
           ssh2_auth_password($connection, $gwUser, $gwPwd);
           //mkdir(/tmp/moslehpour/testscripts, 0777, true);
           //ssh2_scp_recv($connection, './aust/conf/hosts', $workDir . '/hosts')
           if(isset($_POST['option']) && $_POST['option'] == 1) { 
                $stream = ssh2_exec($connection, "/tmp/user/testscripts/up.sh");
                stream_set_blocking($stream, true);
                $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
                echo stream_get_contents($stream_out);

           }

           if(isset($_POST['option'])  && $_POST['option'] == 2) { 
                $stream = ssh2_exec($connection, "/tmp/user/testscripts/down.sh");
                stream_set_blocking($stream, true);
                $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
                echo stream_get_contents($stream_out);
           }

     }


}