php - 如果isset $ _POST

我在一个页面上有一个表单,提交到另一个页面。 在那里,它检查输入邮件是否已填满。 如果是,那么做一些事情,如果没有填补,做其他事情。 我不明白为什么它总是说它被设置,即使我发送一个空表格。 缺少什么或错了什么?

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";
}
Nrc asked 2019-09-11T02:30:51Z
14个解决方案
204 votes

把它改成这个:

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

所以总是设置!empty(),但其内容可能为空。

由于!empty()已经检查了值是否设置,您还可以使用此版本:

if (!empty($_POST["mail"])) {
    echo "Yes, mail is set";    
} else {  
    echo "N0, mail is not set";
}
oopbase answered 2019-09-11T02:31:18Z
21 votes

请使用$_SERVER['REQUEST_METHOD'] == 'POST'而不是isset。isset返回true为$_POST因为$_POST数组是超全局并且始终存在(设置)。

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

Nemoden answered 2019-09-11T02:31:50Z
5 votes

来自php.net,isset

如果var存在且值不是NULL,则返回TRUE,FALSE   除此以外。

空白空间被视为已设置。 您需要使用empty()来检查所有null选项。

janenz00 answered 2019-09-11T02:32:27Z
2 votes

如果您将表单发送为空,则仍会发送$ _POST ['mail'],但该值为空。 要检查字段是否为空,您需要检查

if(isset($_POST["mail"]) && trim($_POST["mail"]) != "") { .. }
mboldt answered 2019-09-11T02:32:52Z
2 votes

将以下属性添加到输入文本窗体: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";    
}
Kushan Mehta answered 2019-09-11T02:33:25Z
2 votes

你可以简单地使用:

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'];
}
Pedro Lobito answered 2019-09-11T02:33:58Z
2 votes

请使用!empty()而不是isset().因为isset()在您的情况下将始终返回true。

if (!empty($_POST["mail"])) {
    echo "Yes, mail is entered";    
} else {  
    echo "No, mail is not entered";
}
Nadim Tareq answered 2019-09-11T02:34:25Z
1 votes

也许你可以尝试这个:

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

Nica answered 2019-09-11T02:34:51Z
1 votes
<?php
    if(isset($_POST['mail']) && $_POST['mail']!='') {
        echo "Yes, mail is set";
    }else{
        echo "N0, mail is not set";
    }
?>
Asep Nurjaman answered 2019-09-11T02:35:10Z
0 votes

要回答已发布的问题:isset和empty一起给出了三个条件。 这也可以由带有ajax命令的Javascript使用。

$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."!";
Vinnarian answered 2019-09-11T02:35:37Z
0 votes

你可以试试这个:

if (isset($_POST["mail"]) !== false) {
    echo "Yes, mail is set";    
}else{  
    echo "N0, mail is not set";
}
akash ujjwal answered 2019-09-11T02:35:57Z
0 votes
<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";
}
?>
Thilina answered 2019-09-11T02:36:16Z
0 votes

你可以试试,

 <?php

     if (isset($_POST["mail"])) {
            echo "Yes, mail is set";    
        }else{  
            echo "N0, mail is not set";
        }
  ?>
Vimukthi Guruge answered 2019-09-11T02:36:42Z
0 votes

让我们在step2.php中认为这是你的HTML表单

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>

我认为你的数据库需要它,所以你可以将你的HTML表格值分配给php变量,现在你可以使用Real Escape String,下面必须是你的

step2_check.php

if(isset($_POST['mail']) && !empty($_POST['mail']))
{
$mail = mysqli_real_escape_string($db, $_POST['mail']);
}

其中$ db是您的数据库连接。

AleksAnderson IT answered 2019-09-11T02:37:34Z
translate from https://stackoverflow.com:/questions/13045279/if-isset-post