empty($_POST) === FALSE doesn't work as it should - php

I was testing the empty function in php, but it doesn't work, because the if statement is accessible whether the condition is false or true.
<?php
if(empty($_POST) === false){
echo 'text';
}
?>
<form action="index.php" method="post">
Username:
<input type="text" name="text">
<input type="submit" name="submit">
</form>
The echo is executed even if the input is empty.
Why ??

That's because $_POST will NEVER be empty after receiving a POST request from a form with input values inside it.
If you var_dump yours, you'll see:
textarray(2) { ["text"]=> string(0) "" ["submit"]=> string(12) "Submit Query" }
Even your submit input is sent (as it is part of the form). And even without receiving anything, $_POST will return an empty set array, so neither isset is a good option to check POST fail or success.
You need to evaluate a specific field, like $_POST['text'], for emptiness instead.

if(isset $_POST["text"] && $_POST["text"] !=null)
also, try using == instead of ===
== will cast the value of $_POST, so if it's NULL, it will be evaluated as false instead

Related

Using ISSET() and the value is never False with Html Select Option code

I am trying to better understand the ISSET() function. It is my understanding that when used properly, I can determine if a "submit" button was pressed or not because the function determines true or false. I have written some html code that uses the HTML Tag and HTML tag type submit. What I was trying to test was whether the ISSET function would return False while the button was not pressed and True when it was pressed.
Using Wordpress Plugin called "Woody snippets" it allows the combination of HTML and PHP in a shortcode.
Please see my code below:
I have been trying to follow a validation video on youtube = https://youtu.be/x_-HdHijmkw
I did everything line for line and initially it worked, upon refreshing the page, it did not clear all of the variables and the isset function acted as if the boolean was true. I thought it was my cache, so I cleared it. Still the ISSET funtion acts as if the boolean is true.
<?php
if (isset($_GET['fname']) && isset($_GET['lname'])) {
$fname = $_GET['fname'];
$lname = $_GET['lname'];
echo "Hello ".$fname;
}
?>
<form action='' method='get'>
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
<input type="submit" value="Greet Me"><br>
</form>
I expected upon refresh that the isset() function and the variables used to determine true or false would be false until I entered values and pressed the button again.
Pre info: isset($a) && isset($b) is equivalent to isset($a,$b)
isset() proofs the existence of a var, not if it is empty. Try
if ( isset($_GET['fname'],$_GET['lname'])
&& strlen($_GET['fname'])
&& strlen($_GET['lname']) { ...
isset() checks whether the variable exists and will still return true for empty strings, 0, and NULL values. A quick solution would be to use !empty() instead:
if (!empty($_GET['fname']) && !empty($_GET['lname']))
See: In where shall I use isset() and !empty()

PHP isset not working in POST method

I tried to make a form with a post method, and i want to check if the fields are filled with isset(). But even if the fields are empty, the isset returns true.
Here is a simplified code: (name of the page: test.php, the post method is directed to itself)
if (isset($_POST['test'])) {
echo 'field set';
}
?>
<form method="post" action="test.php">
<input type="text" name="test"/>
<input type="submit" value="Submit"/>
</form>
When you submit, it always echoes 'field set', even if you didn't enter anything in the "test" input.
Thanks for your answers !
So check whether $_POST['test'] is empty or not and try like this
if (isset($_POST['test']) && trim($_POST['test']) != '') {
echo 'field set';
}
You can also use empty,but Note that "0" is also "empty" and along with it its better to trim the inputs to remove spaces. Another way is to check the length of input using strlen
if (isset($_POST['test']) && trim(strlen($_POST['test'])) > 0) {
echo 'field set';
}
try with empty() to check blank cause after submit form $_POST['test'] always found in isset() even it's blank. so need to check with empty()
if (!empty($_POST['test'])) {
echo 'field set';
}
0 would be empty/blank with checking empty()
You can do this with empty() method.try with this code.
ex : echo !empty($_POST['test']) ? "field set" : "" ;
It will echo that you field is set because all you are checking for if is the array key test exists with isset(). As long as you have a field in your form with the name test, it will create an empty key, thus resulting in it being set.
What you would rather like is to see if the key is set, and if the key also is empty or not.
This you can do by:
if( isset( $key['test'] ) && $key['test'] !== "" ){
//Do something
}

isset function doesn't work on my form and form is nod getting submitted

I am submitting form to page and checking if submit button value InsertMe isset but non of the code inside is executed
<?php
if (isset($_POST['InsertMe'])){
//code to execute
echo "Test";
}
?>
and insert buttons looks like that
<input style="float:right;" name="InsertMe" id="InsertMe" type="submit" class="rectangular-button" value="Add User" />
if (!isset($_POST['InsertMe']) || empty($_POST['InsertMe'])) {
// error message here
} else {
// What you want to do if not empty and is set.
}
This code will check if the variables is set and if if it's empty.
"||" is the PHP operator to check or. So in this case it's checking if it's set OR empty.
Make sure you have the form tag set to post.
<form method="post">
you are selecting Id I think we should be use name tag parameters in isset()

Simple PHP form submitting - what's wrong with this conditional if and logical &&?

The problem:
if i submit the html form and a textbox is left blank
then i don't want it to proceed to the echo segment,
but the problem is it does proceed.
<?php
if(!isset($_POST['submit']) && empty($_POST['moon']) && empty($_POST['planet']))
{
?>
<form name="form2" method="post" action="<?php echo($_SERVER["PHP_SELF"]);?>">
<div>
Write a planet name: <input name="planet" type="text"><br>
Its moon: <input name="moon" type="text">
</div>
<div>
<input type="submit" name="submit" value="submit">
</div>
</form>
<?php
}else{
echo("Planet: ".$_POST['planet']. "<br>");
echo("Moon: ". $_POST['moon'] . "<br>");
echo "Successful.";
}
?>
As you know isset() determines if a variable is set and not null but doesn't check if it's empty.
While logic seems my if statement, I modified it from:
if(!isset($_POST['submit']) && empty($_POST['moon']) && empty($_POST['planet']))
To:
if(!isset($_POST['submit']) && ($_POST['planet']=='') && ($_POST['moon']==''))
if(!isset($_POST['submit']))
if(!isset($_POST['planet']) && !isset($_POST['moon']))
if(empty($_POST['moon']) && empty($_POST['planet']))
and none of them worked.
So am I doing something wrong with my if statement? how can I not let it proceed to the Else segment while a textbox is empty? without more if and no nested statements please.
When you submit a form, the submit button will be set, so isset($_POST['submit']) will be true, therefore !isset($_POST['submit']) will be false.
When doing an if statement with the && comparison, all conditions must be true in order to execute that block of code, otherwise it goes to the else statement.
What you need to do is actually have 2 comparison checks. Once to see if the form was never submitted and one to see if it was, and the text boxes are empty:
<?php
// Check if form was submitted
if(!isset($_POST['submit'])
{
// Display the form
}
else
{
// Form was submitted, check if values are empty
if(trim($_POST['planet'])=="" || trim($_POST['moon'])=="")
{
// One or more value is empty, do something
}
else
{
// Process form
}
}
?>
I realize you are trying to avoid nesting, but in order for the logic to flow smoothly, and the code to remain readable, this is a necessary evil.
Change
if(!isset($_POST['submit']) && empty($_POST['moon']) && empty($_POST['planet']))
To
if(!isset($_POST['submit']) || (empty($_POST['moon']) || empty($_POST['planet'])))
Then if you submit with either textbox being empty, it will redisplay the form. If both textboxes have been filled in, you will see the else part.
Your problem is that if(!isset($_POST['submit'])) is always set when the form is submitted - so that is true. You also might want to change the && to ||. By using || you say OR, so say if anyone is empty, then do this, else do that.

isset isn't working when submitting a form

I want to validate form fields when submitting the form, I use isset() to check if the field has a value or not. The problem I face that it returns true if the field has a value of does not !!!!!
here's HTML code
<form action="handler.php" method="post">
<label>userName: </label><input type="text" name="fname" /><br/>
</form>
and here's php code
<?php error_reporting(E_ALL);
if(validateForm()){?>
Welcome <?php echo $_POST["fname"]; ?>.<br />
}
function validateForm(){
$empty=false;
$empty=isset($_POST['fname'])?'true':'false';
echo 'empty>>>>'.$empty;
return $empty;
}
?>
What you want to use is empty :
function validateForm(){
return !empty($_POST['fname']);
}
edit : removed isset() as it wasn't necessary. Thanks to #Julian.
When your doing an input form, and the input textfield is empty, it's not NULL. isset checks if it's NULL or not. Returns true if not NULL and returns false if NULL.
I would check if it's empty, using empty(). It checks if the variable passed to it is empty or not. Returns true if empty, false if non empty.
Even if the field has no value, it is set with the value of "" (a string with the length 0). If you want to check if there is a value which is usable, either use empty() or check the length of the string.
use
isset($_POST['fname']) && !empty($_POST['fname'])
if not sure whats inside $_POST ,just print it
var_dump($_POST)

Categories