-7
<form class="form-inline" action="form.php">
            <div class="form-group">
            <label for="exampleInputName2">Name</label>
            <input type="text" name="username" class="form-control" id="exampleInputName2" placeholder="Jane Doe">
            </div><br/><br/>
            <div class="form-group">
            <label for="exampleInputEmail2">Email</label>
            <input type="email" name="email" class="form-control" id="exampleInputEmail2" placeholder="jane.doe@example.com">
            </div><br/><br/>
            <button type="submit" class="btn btn-default">Subscribe</button>
            </form>




<?php echo $_GET["username"]; ?><br/><br/>  : This is working.
<?php echo $_POST["username"]; ?><br/><br/> : this is not working.

shows error :Undefined index: username in C:\xampp\htdocs\project1\form.php on line 9

  • you can not have have post and get together for same form. – Chetan Ameta Mar 10 '17 at 05:41
  • 1
    post the code of your form..if you are using get as method post wont work.. – Vimal Mar 10 '17 at 05:42
  • Welcome to SO. Please read: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and also [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – M. Eriksson Mar 10 '17 at 05:49
  • does the field on your form called username have username spelled correctly? Hard to tell without the form. – sbowde4 Mar 10 '17 at 06:06

2 Answers2

1

If you want to access the variable via the $_POST array, you have to specify post as the method in your form (or in whatever mechanism you're using to hit the file, like an AJAX request or something, you'd need to specify "post" accordingly):

<form method="post" action="your-file-or-route">

If you don't want to worry about that, or you don't care whether it got to you via POST or GET, PHP does offer a $_REQUEST array, which combines the two (and $_COOKIE). See here: http://php.net/manual/en/reserved.variables.request.php

(You may also want to check that the value exists in whatever array you use before you try to display it, with isset() [1] or empty() [2] or array_key_exists() [3].)

Davis
  • 856
  • 4
  • 11
0

To use $_POST[] you have to set the form method="post" like:

<form method="post" action="">
...
</form>
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59