2012-10-24 52 views
66

我有一個表單提交到另一個頁面。在那裏,它檢查輸入的郵件是否被填充。如果是這樣,那麼做一些事情,如果沒有填充,做一些其他的事情。我不明白爲什麼它總是說它已經設置,即使我發送一個空的表單。什麼是缺失或錯誤?

step2.php:

<form name="new user" method="post" action="step2_check.php"> 
<input type="text" name="mail"/> <br /> 
<input type="password" name="password"/><br /> 
<input type="submit" value="continue"/> 
</form> 

step2_check:

if (isset($_POST["mail"])) { 
    echo "Yes, mail is set";  
}else{ 
    echo "N0, mail is not set"; 
} 
+4

所有與文字輸入等,並出現在您的形式文本域將被提交到,即使它們的值是空字符串的服務器。 – hypeJunction

回答

173

更改它以這樣的:

if (isset($_POST["mail"]) && !empty($_POST["mail"])) { 
    echo "Yes, mail is set";  
}else{ 
    echo "N0, mail is not set"; 
} 

所以$ _ POST始終設置,但其內容可能是空的。 !

由於空()已檢查值是否設置,您也可以使用此版本:如果VAR存在,並且具有比其他值

if (!empty($_POST["mail"])) { 
    echo "Yes, mail is set";  
}else{ 
    echo "N0, mail is not set"; 
} 
+2

這兩個選項都正常工作。很清楚,很好地解釋。謝謝! – Nrc

+7

這個比較表對於像這樣的事情非常有幫助http://php.net/manual/en/types.comparisons.php –

+1

小事情......我認爲在這種情況下最好避免使用'!'運算符(如果(空()){/ *否* /}其他{/ *是* /}' – MrWhite

18

使用!empty而不是isset。 isset對於$_POST返回true,因爲$_POST數組是超全局的並且始終存在(set)。

或者更好地利用$_SERVER['REQUEST_METHOD'] == 'POST'

+2

你應該提到使用'isset'和'!empty'來防止錯誤。編輯:哎呀,它顯然是每天學習[Ref](http://kunststube.net/isset/) – Touki

+1

什麼錯誤? '空'抑制錯誤 – Nemoden

+0

空不會拋出錯誤(對於未設置的變量)@Touki – PeeHaa

2

如果您發送的形式空,$ _ POST [「郵件」]仍將被髮送,但值爲空。要檢查該字段爲空,你需要檢查

if(isset($_POST["mail"]) && trim($_POST["mail"]) != "") { .. } 
+0

我複製粘貼代碼,我認爲它不起作用?你檢查過嗎? – Nrc

4

From php.net,isset

返回TRUE NULL,否則爲 。

空白空間被視爲集合。你需要使用empty()來檢查所有的null選項。

1

也許你可以試試這個:

if (isset($_POST['mail']) && ($_POST['mail'] !=0)) { echo "Yes, mail is set"; } else { echo "No, mail is not set"; } 

2

以下屬性添加到輸入文本形式:required="required"。 如果表單未填寫,則不允許用戶提交表單。

你的新代碼將是:

<form name="new user" method="post" action="step2_check.php"> 
<input type="text" name="mail" required="required"/> <br /> 
<input type="password" name="password" required="required"/><br /> 
<input type="submit" value="continue"/> 
if (isset($_POST["mail"])) { 
    echo "Yes, mail is set";  
} 
0

要回答這個問題發佈:isset和空一起給出三個條件。 Javascript也可以使用ajax命令來使用它。

$errMess="Didn't test"; // This message should not show 
if(isset($_POST["foo"])){ // does it exist or not 
    $foo = $_POST["foo"]; // save $foo from POST made by HTTP request 
    if(empty($foo)){  // exist but it's null 
     $errMess="Empty"; // #1 Nothing in $foo it's emtpy 

    } else {    // exist and has data 
     $errMess="None"; // #2 Something in $foo use it now 
     } 
} else {     // couldn't find ?foo=dataHere 
    $errMess="Missing"; // #3 There's no foo in request data 
    } 

echo "Was there a problem: ".$errMess."!"; 
2

您可以簡單地使用:

if($_POST['username'] and $_POST['password']){ 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
} 

另外,使用empty()

if(!empty($_POST['username']) and !empty($_POST['password'])){ 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
} 
+0

根據[PHP類型比較表](http://php.net/manual/en/types.comparisons.php)你是絕對正確的。一個簡單的布爾函數執行空函數的工作來檢查空字符串。 – viery365

0

你可以試試這個:

if (isset($_POST["mail"]) !== false) { 
    echo "Yes, mail is set";  
}else{ 
    echo "N0, mail is not set"; 
} 
+0

你需要解釋你的答案。 SO存在不只是回答問題,而是教育人們 – Machavity

0
<form name="new user" method="post" action="step2_check.php"> 
    <input type="text" name="mail" required="required"/> <br /> 
    <input type="password" name="password" required="required"/><br /> 
    <input type="submit" value="continue"/> 
</form> 

<?php 
if (!empty($_POST["mail"])) { 
    echo "Yes, mail is set";  
}else{ 
    echo "N0, mail is not set"; 
} 
?> 
0

你可以試試,

<?php 

    if (isset($_POST["mail"])) { 
      echo "Yes, mail is set";  
     }else{ 
      echo "N0, mail is not set"; 
     } 
    ?> 
2

使用!empty()而不是isset()。因爲isset()將始終在您的情況下返回true。

if (!empty($_POST["mail"])) { 
    echo "Yes, mail is entered";  
} else { 
    echo "No, mail is not entered"; 
} 
0
<?php 
    if(isset($_POST['mail']) && $_POST['mail']!='') { 
     echo "Yes, mail is set"; 
    }else{ 
     echo "N0, mail is not set"; 
    } 
?> 
+3

你應該解釋爲什麼你的代碼回答OP的問題。 – Markus