-3

my html form not sending food val to php to print it on browser so i want enter some data into html page and i want the php code print it the value in php code is food !

 <?php


    if( isset($_GET['submit']) )
    {   
        echo $food;
    }

    ?>

    <html>
    <form action="" method="get">
    <br>
    <input type="text" name="food" value="enter food">
    <br>
     <input type="submit" name="submit" value="send"></input>
    <input type=reset value="reset">
    </form>
    </html>
rickdenhaan
  • 10,857
  • 28
  • 37
teko
  • 37
  • 8
  • value from food field exists in `$_GET['food']` – Artem Ilchenko Oct 31 '17 at 22:22
  • 1
    I would advise you to use `if( !empty($_GET['food'] )){ ... }` instead. `isset()` only checks if the variable exists. Which always returns true when you submit the form. Even if the food field is empty. `empty()` will also check if something was filled in or not. – icecub Oct 31 '17 at 22:36

2 Answers2

4

You should use food $_GET["food"] instead of $food. Here is code:

    if( isset($_GET['submit']) )
    {   
        echo $_GET['food'];
    }
Dmitriy Buteiko
  • 624
  • 1
  • 6
  • 14
  • Tnx But Why i cant just do – teko Oct 31 '17 at 22:34
  • @teko because $_GET is global variable, that contains variables from GET request.$food is just undefined variable. – Dmitriy Buteiko Oct 31 '17 at 22:39
  • @teko You can't use `$food` because it doesn't exist. By submitting your form, you are creating the `$_GET['food']` variable. So it exists. But you didn't create the `$food` variable. If you want to use that, you have to create it first by doing `$food = $_GET['food']`. Now you have created the `$food` variable and put the value of `$_GET['food']` inside it. – icecub Oct 31 '17 at 22:39
  • yeah thanks so much for this info.. – teko Oct 31 '17 at 22:43
-1

action should be action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"

After you do that you can check and access your variables with $_GET['food'].

jscul
  • 748
  • 1
  • 10
  • 25
  • blank action is fine - that is always submit to self –  Oct 31 '17 at 22:32
  • Maybe for student projects... you need to filter form inputs on submit otherwise you'll be vulnerable to malicious injections. – jscul Oct 31 '17 at 22:45
  • No. You are filtering before submit, i can still submit a form( or make any request) to any url on your site. You filter on receipt of the data. –  Oct 31 '17 at 22:56