2011-08-13 194 views
0

嘿傢伙我試圖讓一個PHP腳本檢查文本框是否爲空,我使用下面的代碼。檢查文本框是否爲空?

<?php 
$dbname = $_POST['db_name']; 
$dbuser = $_POST['db_user']; 
$dbpass = $_POST['db_pass']; 
$username = $_POST['username']; 
$password = $_POST['password']; 

if($dbname == "" || $dbuser == "" || $dbpass == "" || $username == "" || $password == "") 
{ 
Echo("Missing Information!"); 
}else{ 
Echo("Success!"); 
} 
?> 

下面是我的表單代碼: 「缺少信息」

<form method="post" style="padding-left: 50px" action="install_submit.php"> 
<h2>Database connection settings</h2> 
<label for="db_name">Database name:</label> 
<input type="textbox" id="db_name" name="db_name" /><br/> 
    <label for="db_user">Database username:</label> 
    <input type="textbox" id="db_user" name="db_user" /><br/> 
<label for="db_pass">Database password:</label> 
<input type="password" id="db_pass" name="db_pass" /><br/> 
<h2>CPanel settings</h2> 
<label for="username">Username:</label> 
<input type="textbox" id="username" name="username" /><br/> 
<label for="password">Password:</label> 
<input type="password" id="password" name="password" /><br/> 


<input type="submit" name="submit_install" value="Install" /> 
</form> 

有了它總是返回即使當所有的文本框都填寫了。

我在做什麼錯?

+1

執行'print_r($ _POST)'來檢查它沒有問題。 – JJJ

回答

4

使用empty()代替,

if (empty($dbname) || empty($dbuser) || empty($dbpass) || empty($username) || empty($password)) { 
    //Empty 
} 
else { 
    //Not Empty 
} 

檢查看到的輸入name小號是正確的爲好。

+0

沒有工作,但我從代碼添加到原始帖子,如果有幫助。 –

+0

你的代碼(在本地開發環境中複製/粘貼和測試)工作正常。 –

+0

確保你接受正確的答案,如果它爲你工作@DuncanP –

0

爲了調試嘗試做一個

print_r($_POST); 

看看你實際收到的$ _POST變量,然後嘗試在你的if語句每次一個比較。 也許它讓你看到幕後。

0

替換爲&檢查您是否獲取所有參數值。

<?php 
$dbname = $_POST['db_name']; 
$dbuser = $_POST['db_user']; 
$dbpass = $_POST['db_pass']; 
$username = $_POST['username']; 
$password = $_POST['password']; 

echo "<pre>"; 
print_r($_POST); 
echo "</pre>"; 
die(); 

if($dbname == "" || $dbuser == "" || $dbpass == "" || $username == "" || $password == ""){ 
    echo("Missing Information!"); 
}else{ 
    echo("Success!"); 
} 
?> 
0

要檢查發佈的數據u可以使用isset()

確定一個變量被設置,而不是NULL

if(!isset($dbname) || trim($dbname) == '') 
{ 
    echo "You did not fill out the required fields."; 
} 

trim()關鍵字除去其通過empty()忽略空格。