问题
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\";
}
回答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";
}
回答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'
回答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.
回答4:
If you send the form empty, $_POST['mail'] will still be sent, but the value is empty. To check if the field is empty you need to check
if(isset($_POST["mail"]) && trim($_POST["mail"]) != "") { .. }
回答5:
Add the following attribute to the input text form: required="required"
.
If the form is not filled, it will not allow the user to submit the form.
Your new code will be:
<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";
}
回答6:
You can simply use:
if($_POST['username'] and $_POST['password']){
$username = $_POST['username'];
$password = $_POST['password'];
}
Alternatively, use empty()
if(!empty($_POST['username']) and !empty($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
}
回答7:
Use !empty()
instead of isset()
. Because isset()
will always return true in your case.
if (!empty($_POST["mail"])) {
echo "Yes, mail is entered";
} else {
echo "No, mail is not entered";
}
回答8:
Maybe you can try this one:
if (isset($_POST['mail']) && ($_POST['mail'] !=0)) { echo "Yes, mail is set"; } else { echo "No, mail is not set"; }
回答9:
<?php
if(isset($_POST['mail']) && $_POST['mail']!='') {
echo "Yes, mail is set";
}else{
echo "N0, mail is not set";
}
?>
回答10:
To answer the posted question: isset and empty together gives three conditions. This can be used by Javascript with an ajax command as well.
$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."!";
回答11:
You can try this:
if (isset($_POST["mail"]) !== false) {
echo "Yes, mail is set";
}else{
echo "N0, mail is not set";
}
回答12:
<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";
}
?>
回答13:
You can try,
<?php
if (isset($_POST["mail"])) {
echo "Yes, mail is set";
}else{
echo "N0, mail is not set";
}
?>
回答14:
Lets Think this is your HTML Form in step2.php
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>
I think you need it for your database, so you can assign your HTML Form Value to php Variable, now you can use Real Escape String and below must be your
step2_check.php
if(isset($_POST['mail']) && !empty($_POST['mail']))
{
$mail = mysqli_real_escape_string($db, $_POST['mail']);
}
Where $db is your Database Connection.
回答15:
Check to see if the FORM has been submitted first, then the field. You should also sanitize the field to prevent hackers.
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" id="SubmitForm" name= "SubmitForm" value="continue"/>
</form>
step2_check:
if (isset($_POST["SubmitForm"]))
{
$Email = sanitize_text_field(stripslashes($_POST["SubmitForm"]));
if(!empty($Email))
echo "Yes, mail is set";
else
echo "N0, mail is not set";
}
}
来源:https://stackoverflow.com/questions/13045279/if-isset-post