-2

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

StarScrime
  • 31
  • 1
  • 2
  • 9

3 Answers3

0

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.

Martin
  • 19,815
  • 6
  • 53
  • 104
  • OP has a input with the name v: http://stackoverflow.com/questions/28282233/isset-function-undefined-variables-variables#comment44918848_28282233 – Rizier123 Feb 02 '15 at 16:35
  • And after that im getting another error 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 – StarScrime Feb 02 '15 at 16:36
  • It's not stated on his question. And fits the error message. Anyhow, his comment was written as I was writing my answer so I will update my answer as he updates his question.... – Martin Feb 02 '15 at 16:36
0

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;
Linesofcode
  • 4,014
  • 8
  • 47
  • 88
0

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>