[英]HTML form submit to PHP
好的,所以我有一個名為proxy.php的文件,其中包含這些內容,我想要做的是,如果任何表單填充了一個值並提交,那么“if”檢查應該變為true並運行命令但是我有一個問題,即使我提交了一個值,它也沒有進入“if”檢查。 如果我將命令從“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');
?>
可以嘗試使用isset來檢查細節。
if(isset($_POST['Name of field to check for'])){
////CODE HERE
}
另一種方法是檢查表單是否已提交然后執行某些操作
if(empty($_POST) === false){
///CODE HERE
}
值$ _POST ['hport1']為null,因為您從html發布'hport'。 嘗試改變。
if($_POST['hport']){
echo $ssh->exec('ls');
echo $ssh->exec('screen -S Proxy1 -X quit');
echo $ssh->exec('Run these commands');
}
如果問題仍然存在,請使用isset($ _ POST ['hport'])檢查變量'hport'的值是否設置。 您可以手動檢查POST值,使用
<?php var_dump($_POST); ?>
要么
<?php
echo '<pre>' . print_r($_POST) . '</pre>';
?>
用於以可讀格式顯示$ _POST數組值。 希望這會幫助你。
使用PHP的內置函數isset()和empty()來檢查變量..
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');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.