is if ($_POST['submit']) ... same as if (isset($_POST['submit']) ... or it means something else? Which one i should use to check if form was submitted?
Is there some way to pull form name?
Use if ($_SERVER['REQUEST_METHOD'] == 'POST'). All other methods are flawed. What if you rename the submit button?
To get the form name you can either:
1) use a hidden input field whose value is the same as the form name
2) set the name of the submit button to the form name
The actual form name is not sent in the POST or GET variables.
isset
Determines if a variable is set and is not NULL
Boolean cast (same as !empty($var))
empty returns FALSE if var has a non-empty and non-zero value.
The following things are considered to be empty:
* "" (an empty string)
* 0 (0 as an integer)
* 0.0 (0 as a float)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)
Pages can have more than one form. It's best to give your submit element a value as well:
<input type="hidden" name="submit" value="login" />
Then in your PHP code, you can:
if (isset($_POST['submit']) && $_POST['submit'] == 'login') {
}
if(isset($_POST['submit'])) checks if the variable exists or in this case if the key exists under the array and is not null.
if($_POST['submit']) will check if the value of $_POST['submit'] is equal to true (or some other value that evaluates to true).
the second method there, will throw a notice error if the variable is not set. You should use isset to check if a variable is set, that's what it is for.
they are the same but using if(isset($_POST['submit']) or if(!empty($_POST['submit']) are better
Related
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()
I am trying simple web form action in PHP, whatever input given to in the input field output value is 1. and undefined index error for username, password when isset not present before the $_POST
<html>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit"></form>
</html>
<?php
$usernmae=isset($_POST["username"]);
$pass=isset($_POST["password"]);
echo $usernmae," ",$pass;
?>
<?php
/*** if else in a single statement ****/
$username = isset($_POST["username"]) ? $_POST["username"] : "";
$pass = isset($_POST["password"]) ? $_POST["password"] : "";
echo $username." - ".$pass;
?>
Dont use
$usernmae=isset($_POST["username"]);
$pass=isset($_POST["password"]);
Put only
$usernmae=$_POST["username"];
$pass=$_POST["password"];
Because isset() gives boolean values i.e. 1 or 0. In your case isset($_POST["username"]) checks that you have put values in this field or not. if there is some values in username field then it return 1 otherwise returns 0.
It should be
$usernmae= $_POST["username"];
$pass= $_POST["password"];
echo $usernmae," ",$pass;
isset($_POST["username"]) and isset($_POST["password"]) return 1 if evaluation is TRUE since isset() return boolean.
Others have told you how to solve the problem. I will tell you what the problem is.
Problems
You need to know following things:
PHP's boolean to string implicit conversion
When implicitly converting a boolean to a string, PHP acts a bit weird.
$testVar = false;
echo (string)$testVar; //will output a empty string (as that evaluates to false)
$testVar = true;
echo (string)$testVar; //will output 1 (as that evaluates to true)
How isset() works
According to the PHP Manual,
Returns TRUE if [variable supplied as argument] exists and has value other than NULL, FALSE otherwise.
What happens when you try to access a $_POST parameter
As you very well know, $_POST parameters are available to a PHP script when,
There is an HTML form, of which action attribute points to the PHP script
User submits the form, with or without data
If the name attribute of a field in the form was "username", you can access this information from the PHP script after the submission of the form like this:
$_POST["username"]
But if the PHP script that processes the $_POST parameters are in the same file as the HTML form, no $_POST parameters will be available to the script when it is initially accessed.
And if you tried to access $_POST parameters when there are none, the result is Undefined index.
What's happening here?
With the understanding of the above three things, we can deduce what's happening in your case.
When your file is loaded on the first time to the browser, PHP interpreter has to interpret it first. But on this first time, no $_POST variables are present. So when you try to access $_POST parameters, it will result in an Undefined index error.
When you wrap the $_POST["..."] expressions in an isset(), isset() will return FALSE, because the parameters are simply not there. Since FALSE is converted to empty string (""), your echo statement produces nothing visible.
When you submit the form, the isset() calls will return TRUE, because the $_POST parameters are present this time. And as described above, TRUE becomes "1" when converted to strings. Hence you get the output 1.
Solution
You must always check before you access $_POST parameters.
$username = "";
if (isset($_POST["username"])) {
$username = $_POST["username"];
}
Or shorthand:
$username = isset($_POST["username"]) ? $_POST["username"] : "";
Why you are using isset($_POST["username"])? The isset() function return false if testing variable contains a NULL value and true otherwise.
Try like
if(isset($_POST))
{
$usernmae=$_POST["username"];
$pass=$_POST["password"];
echo $usernmae," ",$pass;
}
You need to check if a value exists first. If you don't, you will get an error.
thus isset($_POST['VALUE'])
If it exists, get it. If not, set to "empty"
<html>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit"></form>
</html>
<?php
/**
* Created by PhpStorm.
* User: Devunne3
* Date: 25/09/14
* Time: 12:32
*/
$usernmae=isset($_POST["username"])?$_POST['username']:'';
$pass=isset($_POST["password"])?$_POST['password']:'';
echo $usernmae," ",$pass;
?>
Your problem is this line
$usernmae=isset($_POST["username"]);
$pass=isset($_POST["password"]);
You're saving the value of isset(), which is a boolean 0 or 1.
Since the 2 params are set you're getting the 1 (true).
You want to do this:
if(isset($_POST["username"])){
$usernmae = $_POST['username'];
}
and the same for the password.
Replace the bottom 3 lines with
$username=isset($_POST['username'])?$_POST['username']:'';
$pass=isset($_POST['password'])?$_POST['password']:'';
echo $username," ",$pass
The isset() returns boolean, whether the variable is set or not. That makes it return 1. In my answer if the field has value, it will echo the value if not, will echo blank string
try this. and the reason you you are getting 1 and 1, because you are using
$usernmae=isset($_POST["username"]);
That is one couase you set it.
if(!empty($_POST['submit'])){
$usernmae=$_POST["username"];
$pass=$_POST["password"];
echo $usernmae," ",$pass;
}
if(isset($_POST) && !empty($_POST))
{
$usernmae=$_POST["username"];
$pass=$_POST["password"];
echo "User Name is : ".$usernmae,"Password is : ".$pass;
}
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
}
I'm having a real problem with retrieving the value of a radio button using PHP . . . I have two radio buttons as such:
<input name="admin" type="radio" value="1" />Yes
<input name="admin" type="radio" value="0" checked />No
And a conditional PHP statement checking to see if PHP can retrieve any data from it:
if(!empty($_POST['admin'])) {
// do stuff
}
else {
echo "Value not set";
}
the problem is that PHP can seem to return a value for the radio buttons if "yes" is selected, but not if "no" is selected, I've tried removing the "checked" portion, to no avail. I just can't get it to retrieve the "0" value whatever I try.
I remember using PHP arrays to name checkboxes, but this shouldn't be needed for radio buttons surely, as only can be selected at any one time?
Or does PHP just have a problem returning radio buttons with a value of 0?
Or am I doing something horribly wrong without realising it?
PHP function empty will return false for 0, NULL, "", and others.
Quoting from php.net:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
All of these will return (false) therefore nullifying the execution of your if statement.
You will have to modify your condition to reflect a value depending on what you wish to accomplish.
Good luck!
Your problem is that empty() treats zeros as empty values, no matter if it's a number 0, or a string '0'. So, instead of using empty() use isset() and/or direct checks, e.g.
if (isset($_POST['admin'])) {
or
if (isset($_POST['admin']) && $_POST['admin'] == 0) {
I believe the zero is your problem. Try setting them to "Y" and "N" and you should see that the N value comes through. This is because 0 is an empty value in PHP.
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)