如果是设置$ _POST

问题描述 投票:88回答:14

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

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";
}
php isset
14个回答
208
投票

把它改成这个:

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

所以$_POST总是设置,但它的内容可能是空的。

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

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

0
投票

要回答已发布的问题: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."!";

0
投票

你可以试试这个:

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

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";
        }
  ?>

0
投票

让我们在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是您的数据库连接。


21
投票

使用!empty而不是isset。对于$_POST,isset返回true,因为$_POST数组是超全局的并且始终存在(set)。

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


5
投票

From php.net走了

如果var存在并且其值不是NULL,则返回TRUE,否则返回FALSE。

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


2
投票

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

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

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";    
}

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

2
投票

使用!empty()而不是isset()。因为在你的情况下isset()将永远返回true。

if (!empty($_POST["mail"])) {
    echo "Yes, mail is entered";    
} else {  
    echo "No, mail is not entered";
}

1
投票

也许你可以尝试这个:

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


1
投票
<?php
    if(isset($_POST['mail']) && $_POST['mail']!='') {
        echo "Yes, mail is set";
    }else{
        echo "N0, mail is not set";
    }
?>
© www.soinside.com 2019 - 2024. All rights reserved.