Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTML表单提交到PHP_Php_Html_Forms - Fatal编程技术网

HTML表单提交到PHP

HTML表单提交到PHP,php,html,forms,Php,Html,Forms,好的,我有一个名为proxy.php的文件,其中包含这些内容,我想用它做的是,如果任何表单都填写了一个值并提交了,那么“if”检查应该为true并运行命令,但我有一个问题,即使我提交了一个值,它也不会进入“if”检查。如果我将命令置于“如果”检查之外,它们将开始工作,但不会在内部工作 <html> <body> <br> <form action="proxy.php" method="post"> <br> Host Port

好的,我有一个名为proxy.php的文件,其中包含这些内容,我想用它做的是,如果任何表单都填写了一个值并提交了,那么“if”检查应该为true并运行命令,但我有一个问题,即使我提交了一个值,它也不会进入“if”检查。如果我将命令置于“如果”检查之外,它们将开始工作,但不会在内部工作

<html>
<body>

<br>
<form action="proxy.php" method="post">


<br>

Host Port 1: <input type="text" name="hport" />
Server Port 1: <input type="text" name="sport"/>
<br>
Host Port 2: <input type="text" name="hport2"/>
Server Port 2: <input type="text" name="sport2"/>

<br>


<input type="submit" />
</form>

</body>
</html> 

<?php
include('Net/SSH2.php');



$_POST["ip"]="iphere";
$_POST["pass"]="passhere";



$ssh = new Net_SSH2($_POST["ip"]);
if (!$ssh->login('root', $_POST["pass"])) {
    exit('Login Failed');
}


if($_POST['hport1']){

echo $ssh->exec('ls');
echo $ssh->exec('screen -S Proxy1 -X quit');
echo $ssh->exec('Run these commands');
}

if($_POST['hport2']){

echo $ssh->exec('ls');
echo $ssh->exec('screen -S Proxy2 -X quit');
echo $ssh->exec('Run these commands');
}


echo $ssh->exec('exit');


?>



主机端口1: 服务器端口1:
主机端口2: 服务器端口2:

可以尝试使用isset检查详细信息

if(isset($_POST['Name of field to check for'])){
 ////CODE HERE
 }
另一种方法可能是检查表单是否已提交,然后执行某些操作

if(empty($_POST) === false){
///CODE HERE
}

值$\u POST['hport1']为空,因为您是从html发布'hport'。 试着换一下

if($_POST['hport']){
        echo $ssh->exec('ls');
        echo $ssh->exec('screen -S Proxy1 -X quit');
        echo $ssh->exec('Run these commands');
}
如果问题仍然存在,请使用isset($\u POST['hport'])检查变量'hport'的值是否已设置。 您可以手动检查POST值,使用

<?php var_dump($_POST); ?>


用于以可读格式显示$\u POST数组值。希望这将对您有所帮助。

使用PHP的内置函数检查变量


这可能不是全部问题,但是表单的输入是
hport
;您的代码正在查找
hport1
。当您提交表单时,$\u POST['hport']的值是多少?我已经有一段时间没有使用PHP了,但是没有可以使用的isset函数吗?谢谢。是的,这就是问题所在。这只是一个打字错误,在我把它修好后就行了。我们不需要isset函数
<?php
    echo '<pre>' . print_r($_POST) . '</pre>';
?>
if(isset($_POST['hport'] && !empty($_POST['hport'])){
echo $ssh->exec('ls');
echo $ssh->exec('screen -S Proxy1 -X quit');
echo $ssh->exec('Run these commands');
}

if(isset($_POST['hport2'] && !empty($_POST['hport2'])){
echo $ssh->exec('ls');
echo $ssh->exec('screen -S Proxy2 -X quit');
echo $ssh->exec('Run these commands');
}