Undefined index when trying to choose item from array

I’m trying to make a simple php program where you can type in one of the 4 products and it then prints out the price of that product when you type it in the textbox. I keep getting an undefined index for some reason even when I type an item in the box and press enter. For some reason I think that php is not seeing “textbox” as the textbox and just thinks I am trying to access the non-existent index “textbox” in the array.

Capture
https://pastebin.com/raw/4r36H8jb

Notice : Undefined index: textbox in /products.php on line 19

  1. Php Self is vulnerable to an XSS Attack. Remove the action completely
  2. You need to check the SERVER REQUEST METHOD
  3. This is junk: print $_POST[ $prices{ ‘textbox’ } ];
  4. $items is pointless as is.
<?php
$prices =[
    'Batteries' => '$4.99',
    'Radio' => '$14.99',
    'TV' => '$399.99',
    'Resistor' => '$0.05'];

if($_SERVER['REQUEST_METHOD'] == 'POST'){
echo  $prices[$_POST['textbox']];
}
?>
<form method="post">
<input type='text' name='textbox'>
<input type='submit' value='Click me!'>
</form>
1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service