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>
Related
Edit: The issue ended up being because I was trying to resubmit to the same page. See Jeromes solution on here if you're experiencing the same issue.
if( isset( $_GET["fil"]) ){
print "hi";
$test = $_GET["fil"];
print $test;
}
?>
<form action='prob1handlerFilter.php' method='GET'>
<h1>Filter These Results</h1>
<select name='fil' id='fil'>
<?php
$SCORE = $_SESSION["players"];
$uniqueDates = array_values(array_unique($_SESSION["players"]));
sort($uniqueDates);
$x = 0;
while($x < count($uniqueDates))
{
print "<option value = '$uniqueDates[$x]'> $uniqueDates[$x] </option>";
$x = $x + 1;
}
?>
Above is my code. The issue is that even though I can see in the URL string that fil is for sure set(http://127.0.0.1:8080/webserver1/prob1/prob1handlerFilter.php?fil=2012), it is telling me that it is undefined. If I change the if statement to !isset, it will go through and print "hi", but nothing else. This is just confusing me because I can literally see the variable in the URL string and all my other forms are working, just not this one....I'm at a complete loss as to why it's telling me that it's undefined. Full error is
Notice: Undefined index: fil in C:\xampp\htdocs\webserver1\prob1\prob1handler.php on line 70.
But clearly it isn't undefined because it's sitting up in the URL string...
Can you try changing the form action value to
"<?php echo $_SERVER['PHP_SELF'];?>"
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;
On the first page (index.php) I wrote this:
<?php
echo '<form action="test.php" method="get">';
$x=0;
while($x<5){
$x++;
$test='tester'.$x;
echo '<input type="text" name="$test"><br>';
};
echo '<input type="submit" value="submit">';
?>
on the second page (test.php) I wrote this:
<?php
echo $_POST['tester1'];
echo $_POST['tester2'];
echo $_POST['tester3'];
echo $_POST['tester4'];
echo $_POST['tester5'];
?>
and when I tested it I got these errors
Notice: Undefined index: tester1 in C:\xampp\htdocs\test.php on line 2
Notice: Undefined index: tester2 in C:\xampp\htdocs\test.php on line 3
Notice: Undefined index: tester3 in C:\xampp\htdocs\test.php on line 4
Notice: Undefined index: tester4 in C:\xampp\htdocs\test.php on line 5
Notice: Undefined index: tester5 in C:\xampp\htdocs\test.php on line 6
The code below is an example for the real code being meant for users to fill in a number. I want to use these numbers later for calculations so they all need an unique name. The <input> objects are generated via a loop, the amount of times the loop runs is specified by the amount of rows in a database.
The two main issues are that you are using PHP variables inside single quotes, this doesn't pass the actual variable, but the actual name. As an example
$foo = "bar";
echo 'This is $foo';
would print
This is $foo
If you use double-quotes, the variable's content would be passed,
$foo = "bar";
echo "This is $foo"; // You can also do the following: echo 'This is '.$foo;
would print
This is bar
Secondly, you are using method="get" in your form, but trying to retrieve them as POST variables. This means that you have to change it to method="POST".
Another alternative, is to create an array of elements, and use a loop in PHP to retrieve the values. An example given below. The first snippet generates a form with 5 input-fields as an array.
<form action="test.php" method="POST">
<?php for ($x=0; $x<5; $x++) { ?>
<input type="text" name="tester[]" />
<?php } ?>
<input type="submit" name="submit" />
</form>
And in PHP, loop through that array.
<?php
if (isset($_POST['tester']) {
// If the values are set, to avoid Undefined Index notices
foreach ($_POST['tester'] as $value) {
echo $value."<br />";
}
}
?>
This is how it works (index.php):
<?php
echo '<form action="test.php" method="post">';
$x=0;
while($x<5){
$x++;
$test='tester'.$x;
echo '<input type="text" name="'.$test.'"><br>';
};
echo '<input type="submit" value="submit">';
?>
simply append this code to the top of test.php
if(!isset($_POST['tester1']) || !isset($_POST['tester2']) ... etc){
exit(0); //or do something to signal index.php of missing values
}
/* (the rest of the code */
When writing a dynamic page like this, you should always expect there to be a missing variable and code an escape so you never run into an error such as that.
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.
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