I've done a few searches and not come up with anything, I'm sure it's obvious.
Basically I'm trying to work out if anything has been passed via GET from a form.
I know how to check for individual elements, but I just want to do a quick check if anything at all is passed
Cheers
Be careful when using count($_GET). If you submit the form with empty values it will still create keys for the fields, and your count() will be greater than 0 and empty($_GET) will be false.
<?php
print_r($_GET);
?>
<form action="" method="get">
<input type="text" name="name">
<textarea name="mytext"></textarea>
<input type="submit">
</form>
Make sure the fields are actually not empty:
function ne($v) {
return $v != '';
}
echo count($_GET); // prints 2
echo count(array_filter($_GET, 'ne')); // prints 0
This should do the job:
if (!empty($_GET))
{
}
if ( count($_GET) > 0 ) echo("I hear you!");
if(empty($_GET)) { /* no parameters passed*/}
just check the length of the $_GET array via count($_GET).
if none ha passed it should be 0
Simply 'just': if($_GET){ /* parameters passed*/} (for the current request) works to check if any query-string was passed in the GET or POST request.
This is because an empty array is false in a boolean if($x) context.
See: http://php.net/manual/en/types.comparisons.php
So indeed no need for count() or empty().
Actually, I think it is better to check using isset which checks if it is defined and if it is different than null
if(isset($_GET['value_you_looking_for']) {//code logic here}
Check for documentation
Related
I'm trying to update mySQL database with PHP. I got form on index.php. I'm getting value of id from link using GET method and trying to transfer value to another page with input hidden.
When i try to read this value on another page it always returns 1.
Here is my code Index.php
<?php
$usr = $_GET['id'];
echo $usr ;
?>
<form method="post" action="status.php">
<input type="text" name="number"> <br>
<input type="hidden" name="3" value="<?php echo (isset($usr)) ? $usr : '' ?>" />
<input type="submit">
</form>
And Here is status.php
$hello=isset($_POST['3']);
echo $hello ;
echo "<br>";
It's always 1.
isset() return bool so if variable is available the $hello=1 else $hello=NULL
Wrtie POST as
if(isset($_POST['3']))
{
$hello=$_POST['3'];
echo $hello;
}
You'll using isset() function to assign variables which should not be the case:
$hello = isset($_POST['3']);
This will return true if $_POST['3'] is set.
As from the literal meaning isset() means variable is set. It's a BOOL function, which means that it can only return true when set and false when not set/ empty.
Thus, your code should be:
$hello = $_POST['3'];
More information on isset(): http://php.net/manual/en/function.isset.php.
Tip: It is actually not a good idea to put the value in a hidden textbox and then get it using $_GET, as most people can just edit the value in their browser.
It is always returning '1' because of:
$hello=isset($_POST['3']);
The function isset() returns 1 for if the value is set and 0 if it is not set. In binary 1 means "true" and 0 means "false"
Essentially, your code is saying that $_POST['3'] exists.
However, there may be a bigger underling issue: $_POST is an array so using a number as your input name may be considered bad practice. Try using a name using letters instead like:
<input type="hidden" name="hidden_user"/>
I think you are using isset wrong.
This function, isset, is not to grab the value of something, but just to check if this something is defined.
Try this:
$hello=$_POST['3'];
The proper way to get your post parameter in status.php is:
$hello = isset($_POST[3]) ? $_POST[3] : null;
echo $hello;
Getting some issues with my PHP. I'm trying to get assign the value from the text box to a variable so that I can check it against a random number.
This is me initializing my variables:
session_start();
if (!isset ($_SESSION["rand"])) {
$_SESSION["rand"] = rand(0,100);}
$rand = $_SESSION["rand"];
if(!isset ($_SESSION["att"])) {
$_SESSION["att"] = 0;}
This is the form itself:
<form method="post">
<input type="text" id="guess" name="guess"/>
<input type="submit" value="Guess"/>
</form>
and this is the part that is causing the problem:
$answer = htmlspecialchars(trim($_POST["guess"]));
This is the error I'm receiving
I'm trying to achieve it to say nothing really, I know that it's because guess hasn't had anything defined to it but I'm not really sure what to define it while I don't need it.
Since you're using everything in one file <form method="post"> is what gave it away, since the action defaults to "self" if omitted, therefore you need to use a conditional isset() or !empty().
Consult my "footnotes" also.
I.e.:
if(isset($_POST["guess"])){
$answer = htmlspecialchars(trim($_POST["guess"]));
}
or
if(!empty($_POST["guess"])){
$answer = htmlspecialchars(trim($_POST["guess"]));
}
References:
http://php.net/manual/en/function.isset.php
http://php.net/manual/en/function.empty.php
Footnotes:
Even if you were to split those up into two seperate files, it would be best to still use a conditional isset() or !empty().
Use a conditional isset(). It is used to check if a variable is set or not.
When you initially load the page, your POST data is not set and that is the reason why you got that notice.
if(isset($_POST["guess"]) && $_POST["guess"] !=""){
$answer = htmlspecialchars(trim($_POST["guess"]));
}
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 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)
I have some form fields that when a form is submitted creates an array within the $_POST, I needing to check the this array has atleast 4 keys, how can I check that? I have no idea
try:
<?php
if(count($_POST) >= 4):
//Do your stuff
else:
//Do your error stuff
endif;
If you want to check an array within $_POST as apose to $_POST itself use
count($_POST['name_of_key_to_array_you_want_to_count'])
First, to make your work easier, you should change input name into array version. Something like this should work:
<input type='text' name='data[]' value='' />
Then, PHP will do it's magic and all you have to do is:
echo count($_POST['data']);
This is because your data[] form field is changed into array.
Use array_keys and count:
echo count(array_keys($_POST));
Or simply:
echo count($_POST);
because keys are same in number as items.
The count() function returns the length of an array.