我在一个页面上有一个表单,提交到另一个页面。在那里,它检查输入邮件是否已填满。如果是,那么做一些事情,如果没有填补,做其他事情。我不明白为什么它总是说它被设置,即使我发送一个空表格。缺少什么或错了什么?
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";
}
把它改成这个:
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";
}
要回答已发布的问题: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."!";
你可以试试这个:
if (isset($_POST["mail"]) !== false) {
echo "Yes, mail is set";
}else{
echo "N0, mail is not set";
}
<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";
}
?>
你可以试试,
<?php
if (isset($_POST["mail"])) {
echo "Yes, mail is set";
}else{
echo "N0, mail is not set";
}
?>
让我们在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是您的数据库连接。
使用!empty
而不是isset
。对于$_POST
,isset返回true,因为$_POST
数组是超全局的并且始终存在(set)。
或者更好地使用$_SERVER['REQUEST_METHOD'] == 'POST'
如果您将表单发送为空,则仍会发送$ _POST ['mail'],但该值为空。要检查字段是否为空,您需要检查
if(isset($_POST["mail"]) && trim($_POST["mail"]) != "") { .. }
将以下属性添加到输入文本窗体: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";
}
你可以简单地使用:
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'];
}
使用!empty()
而不是isset()
。因为在你的情况下isset()
将永远返回true。
if (!empty($_POST["mail"])) {
echo "Yes, mail is entered";
} else {
echo "No, mail is not entered";
}
也许你可以尝试这个:
if (isset($_POST['mail']) && ($_POST['mail'] !=0)) { echo "Yes, mail is set"; } else { echo "No, mail is not set"; }
<?php
if(isset($_POST['mail']) && $_POST['mail']!='') {
echo "Yes, mail is set";
}else{
echo "N0, mail is not set";
}
?>