If isset $ _POST

I have a form on one page that submits to another page. There he checks to see if incoming mail is full. 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 installed, even if I submit 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"; } 
+92
php isset
Oct 24 '12 at 8:16
source share
14 answers

Change this to this:

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

Thus, $_POST always set, but its contents may be empty.

Since !empty() already checks to see if 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"; } 
+212
Oct 24 '12 at 8:19
source share

Use !empty instead of isset . isset returns true for $_POST , because the $_POST array is superglobal and always exists (is set).

Or it is better to use $_SERVER['REQUEST_METHOD'] == 'POST'

+23
Oct 24 '12 at 8:18
source share

From php.net , isset

Returns TRUE if var exists and has a value other than NULL, FALSE otherwise.

empty space is considered established. To check all null parameters you need to use empty ().

+5
Oct 24 '12 at 8:20
source share

If you submit an empty form, $ _POST ['mail'] will still be submitted, but the value is empty. To check if a field is empty, you need to check

 if(isset($_POST["mail"]) && trim($_POST["mail"]) != "") { .. } 
+2
Oct 24 '12 at 8:19
source share

Add the following text to the text input form: required="required" . If the form is not completed, 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"; } 
+2
09 Oct '14 at 17:08
source share

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']; } 
+2
Apr 30 '15 at 14:26
source share

Use !empty() instead of isset() . Since isset() will always return true in your case.

 if (!empty($_POST["mail"])) { echo "Yes, mail is entered"; } else { echo "No, mail is not entered"; } 
+2
Nov 06 '17 at 17:52
source share

Perhaps you can try the following:

 if (isset($_POST['mail']) && ($_POST['mail'] !=0)) { echo "Yes, mail is set"; } else { echo "No, mail is not set"; } 



+1
01 Oct '14 at 3:34
source share
 <?php if(isset($_POST['mail']) && $_POST['mail']!='') { echo "Yes, mail is set"; }else{ echo "N0, mail is not set"; } ?> 
+1
Nov 10 '17 at 7:23
source share

To answer the question posed: isset and empty together give three conditions. This can be used by Javascript with the ajax command.

 $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 null $errMess="Empty"; // #1 Nothing in $foo it 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 no foo in request data } echo "Was there a problem: ".$errMess."!"; 
0
Apr 05 '15 at 15:49
source share

You can try the following:

 if (isset($_POST["mail"]) !== false) { echo "Yes, mail is set"; }else{ echo "N0, mail is not set"; } 
0
Sep 26 '15 at 20:21
source share
 <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"; } ?> 
0
Jul 11 '16 at 7:31
source share

You can try,

  <?php if (isset($_POST["mail"])) { echo "Yes, mail is set"; }else{ echo "N0, mail is not set"; } ?> 
0
May 10 '17 at 13:36
source share

Let's 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 this is necessary for your database, so you can assign the value of the HTML form to the php variable, now you can use the Real Escape String, and below should 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.

0
Apr 04 '19 at 19:09
source share



All Articles