If isset $_POST
I have a form on one page that submits to another page. There, it checks if the input mail
is filled. If so then do something and if it is not filled, do something else. I don't understand why it always says that it is set, even if I send an empty form. What is missing or wrong?
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.php
:
if (isset($_POST["mail"])) {
echo "Yes, mail is set";
} else {
echo "N0, mail is not set";
}
Solution 1:
Most form inputs are always set, even if not filled up, so you must check for the emptiness too.
Since !empty()
is already checks for both, you can use this:
if (!empty($_POST["mail"])) {
echo "Yes, mail is set";
} else {
echo "No, mail is not set";
}
Solution 2:
Use !empty
instead of isset
.
isset return true for $_POST
because $_POST
array is superglobal and always exists (set).
Or better use $_SERVER['REQUEST_METHOD'] == 'POST'
Solution 3:
From php.net, isset
Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
empty space is considered as set. You need to use empty() for checking all null options.