My code is extremely simple, but I have no idea what I've done to cause this error.
Notice: Undefined index: value in C:\xampp\htdocs\index.php on line 8
<form name="shuffle" action="" method="POST">
<input type="text" name="value">
<input type="submit" value="Shuffle">
</form>
PHP code: echo str_shuffle($_POST['value']);
You have posted form in same file. so you need to check if form is submitted or not.
Try like this:
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
echo str_shuffle($_POST['value']);
}
If you call $_POST['value'] when form has not been previously submitted you get a warning that the key of $_POST is not defined.
Try to define the variable anyway. So that if you have sent the form, take the value of the field, otherwise the value is FALSE
$value = isset($_POST['value']) ? $_POST['value'] : FALSE; //$value is always defined
if($value !== FALSE){
//something like
echo str_shuffle($value);
}
PHP isset function
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;
Hello i have problem with isset id like someone to help me fixin this problem. Its this problem Notice: Undefined variable: v in C:\xampp\htdocs\something\something2.php on line 17 and my code is :
<form id="priklad" method="post">
<label>Obvod kruhu:</label>
<?php IF (isset($_POST['v'])) {
$_POST['v'];
}
?>
<label>r:</label><input type="text" name="r"/>
<input type="submit" value="Vypočti"/></br>
<label><?php echo 2*$v;?> cm</label>
</form>
EDIT:
<form id="priklad" method="post">
<label>Obvod kruhu:</label>
<?php If (is_numeric($_POST['v'])) {
$v = $_POST['v'];
}
?>
<label>v:</label><input type="text" name="v"/>
<input type="submit" value="Vypočti"/></br>
<label><?php echo 2*$v;?> cm</label>
</form>
and im getting these errors
Notice: Undefined index: v in C:\xampp\htdocs\something\something2.php on line 11 and Notice: Undefined variable: v in C:\xampp\htdocs\something\something2.php on line 17
What's actually happening with that error is that your variable $v isn't being initialized because the code has not enter the if condition.
if(is_numeric($_POST['v'])){
$v = $_POST['v'];
}
You're assuming that the condition is_numeric($_POST..) is true but it might not be, which means that the variable $v is never initialized. Therefore is pretty normal the error you are having.
You can continue with your condition but you must check for the variable after it.
if(is_numeric($_POST['v'])){
$v = $_POST['v'];
}
echo 2 * (isset($v) ? $v : 1);
The above code might be improved. A better code/logic would be you to set up the default value of the variable in case the is_numeric returns false.
$v = 1; // default value
if(is_numeric($_POST['v'])){
$v = $_POST['v'];
}
echo 2 * $v;
What is happening is that your value v is not the same as the implied value of $_POST['v'] . As stated by Tim, there is no $v = statement so in effect your echo output is outputting nothing multiplied by 2.
This throws a warning which you have picked up.
So a solution could be:
<form id="priklad" method="post">
<label>Obvod kruhu:</label>
<?php If (is_numeric($_POST['v'])) {
$v = $_POST['v'];
}
?>
<label>r:</label><input type="text" name="r"/>
<input type="submit" value="Vypočti"/></br>
<label><?php echo 2*$v;?> cm</label>
</form>
I have used is_numeric as you are using the echo to produce a value x 2 so makes more sense to check V is a number rather than a string.
UPDATE
Ok, first stop then is to see what is on line 11? is it $_POST['v'] ? If so then you can work back to the source of the cause - replace the line 11 (comment it out, don't delete it) with something like:
print_r($_POST);
This will output all values associated with the array $_POST.
If you do not see a value for 'v' (and I guess you probably won't) then that means this value wasn't set in the previous HTML page so go back and check that your form submits an input such as:
<input name='v' value="something">
Undefined index means the array ( $_POST ) does not contain any information stored under $_POST['v'].
Can you confirm your POST is as intended, and that you do want post which means you need to submit the form rather than GET or another method, such as index.php?v=10 ?
The Notice:
PHP does have notice statements but they're only notices, they are not errors or even warnings, it's just PHP telling you what's going on. This will not break your code, but it could be nice to add this line above your IF statement:
$v = 0;
This establishes the variable $v and gives it a value. Which if $_POST['v'] exists is then updated to reflect that.
Your code must be like this.
<form id="priklad" method="post">
<label>Obvod kruhu:</label>
<?php
$v = '';
If (is_numeric($_POST['v'])) {
$v = $_POST['v'];
}
?>
<label>v:</label><input type="text" name="v"/>
<input type="submit" value="Vypočti"/></br>
<label><?php echo (($v)? 2*$v : 'Its not numeric value');?> cm</label>
</form>
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 wrote a simple php script that basically echos the values put into a form on the page, later, I will have this write to a DB but I was working on troubleshooting it before I did that since I keep getting this warning.
Does anyone know why I am getting it? If I just fill in the fields and submit the form, the script works fine and the warning disappears.
PHP Function:
function quickEntry()
{
$subTitle = $_POST['subTitle'];
$subDetails = $_POST['details']; //This is line 13 in my code
echo "$subTitle";
echo "<br>$subDetails";
}
HTML / PHP Code:
<form method="post" action="">
<hr>
<h1>Quick Entry:<p></h1>
Subject Title:<br> <input type="text" name="subTitle"><br><br>
Subject Details: <p><textarea name="details" placeholder="Enter Details here..."></textarea><p><br>
<input type="submit" name="QuickEntrySubmit" value="Submit Quick Entry" /><br>
</form>
<?php
if (isset($_POST['QuickEntrySubmit']))
{
quickEntry();
}
?>
I know that I could disable warnings and I wouldn't see this, but I really just want to know why php is throwing the warning so I can fix the syntax appropriately and keep my code clean going forward. Full warning is:
Notice: Undefined index: details in C:\xampp\htdocs\test1.php on line 13
Thanks!
The reason why you are getting that error is because you are not checking whether the 'subTitle' and 'details' inputs have values in them.
Your code should work well like this:
function quickEntry(){
$subTitle = isset($_POST['subTitle'])? $_POST['subTitle']: null;
$subDetails = isset($_POST['details'])? $_POST['details']: null ; //This is line 13 in my code
if(!is_null($subTitle) && !is_null($subDetails)){
echo "$subTitle";
echo "<br>$subDetails";
} else{
//blah blah blah
}
You get the warnings because the $_POST variables with the indexes that you're checking for ($_POST['subTitle'] & $_POST['details']) aren't set, so the variables are empty as you reference something that isn't there.
You should do a check to ensure they are set first before trying to assign them to a variable:
$subTitle = (isset($_POST['subTitle']) && !empty($_POST['subTitle'])) ? $_POST['subTitle'] : null;
$subDetails = (isset($_POST['details']) && !empty($_POST['details'])) ? $_POST['details'] : null;
The above code will check to ensure the post index isset() and isn't empty() before assigning it to the variables.
NOTE
The variables are set when you submit the form because you are actually posting the data to the script.
You see the input attribute name? When you submit the form, they are the relevant $_POST indexes.
Just remember to ensure that the values aren't empty if you intend to print them to the markup.