如果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";

}


慕标5832272
浏览 627回答 3
3回答

潇潇雨雨

即使没有填写,大多数表单输入总是被设置的,因此您也必须检查是否为空。由于!empty()已经检查了两者,因此您可以使用以下命令:if (!empty($_POST["mail"])) {&nbsp; &nbsp; echo "Yes, mail is set";&nbsp; &nbsp;&nbsp;} else {&nbsp;&nbsp;&nbsp; &nbsp; echo "No, mail is not set";}

莫回无

使用!empty代替isset。isset返回true,$_POST因为$_POST数组是超全局的,并且始终存在(设置)。还是更好用 $_SERVER['REQUEST_METHOD'] == 'POST'

德玛西亚99

将以下属性添加到输入文本形式:required="required"。如果未填写表格,则不允许用户提交表格。您的新代码将是:<form name="new user" method="post" action="step2_check.php">&nbsp;<input type="text" name="mail" required="required"/> <br /><input type="password" name="password" required="required"/><br /><input type="submit"&nbsp; value="continue"/>if (isset($_POST["mail"])) {&nbsp; &nbsp; echo "Yes, mail is set";&nbsp; &nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP