Nrc Nrc - 4 months ago 42
PHP Question

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:

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

Answer

Change it to this:

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

So $_POST is always set, but its content might be empty.

Since !empty() already checks whether the value is set, you can also use this version:

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