如果 isset $_ POST

我在一个页面上有一个提交到另一个页面的表单。在这里,它检查输入 mail是否已填充。如果是这样,然后做一些事情,如果它没有填补,做别的事情。我不明白为什么它总是说已经设置好了,即使我发送的是一个空表单。什么是缺失或错误的?

返回文章页面

<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>

返回文章页面

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

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'

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"]) != "") { .. }

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";
}

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.

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"; }

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";
}

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."!";

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'];
}

You can try this:

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";
}
?>

You can try,

 <?php


if (isset($_POST["mail"])) {
echo "Yes, mail is set";
}else{
echo "N0, mail is not set";
}
?>
<?php
if(isset($_POST['mail']) && $_POST['mail']!='') {
echo "Yes, mail is set";
}else{
echo "N0, mail is not set";
}
?>

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.

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";
}
}


$-POST METHOD: if we use POST method in the form tag to pass data,we can find data in the server using $_POST array.using this array name we fetch data from server.($_POST['name'])Information sent from a form with the POST method is invisible to others(all names/values are embedded within the body of the HTTP request) and has no limits n the amount of information to send. Example Code:

<html>
<head>
</head>
<body>
<?php
if(isset($_POST['submit']))
{if(isset($_POST['name']) && isset($_POST['roll']))
{echo"<h1>form submitted</h1>";echo"your name is". $_POST['name']."<br>";
echo "your roll number is". $_POST['roll']."<br>";}}
else{?>
<form action="" method="POST">
Name:<input type="text" name="name"><br><br>
Roll:<input type="text" name="roll"><br>
<br><input type="submit" value="submit" name="submit">
</form>
<?php}?>
</body>
</html>