<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
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].)
To use $_POST[]
you have to set the form method="post"
like:
<form method="post" action="">
...
</form>