2013-07-17 215 views
0

好吧,我有一個名爲proxy.php的文件,這些內容以及我想要做的事情是,如果任何表單填充了值並提交,則「if」檢查應該成爲真實的並運行命令,但我有一個問題,即使我提交一個值,它不會進入「如果」檢查。如果我把這些命令放在「if」檢查中,他們就開始工作,但不在裏面。HTML表單提交到PHP

<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'); 


?> 
+0

它可能不是全部問題,但表單的輸入是'hport';你的代碼正在尋找'hport1'。 – andrewsi

+0

當您提交表單時,$ _POST ['hport']的值是多少? – Axarydax

+0

我有一段時間沒有做過PHP,但是沒有可以使用的isset函數嗎? –

回答

0

價值$ _ 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); ?> 

OR

<?php 
    echo '<pre>' . print_r($_POST) . '</pre>'; 
?> 

以可讀格式顯示$ _POST數組值。希望這會幫助你。

+0

謝謝。是的,這是問題。這只是一個錯字,在我修好之後開始工作。我們不需要isset函數 –

1

可以嘗試使用isset來檢查細節。

if(isset($_POST['Name of field to check for'])){ 
////CODE HERE 
} 

另一種可能是檢查是否提交表單,然後做一些

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

使用isset()函數 & 空() PHP的內置函數來檢查變量..

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'); 
}