Reputation: 237
Code:
<html>
<body>
Username: <input type='text' placeholder='Username' name='user' id='user' value='Nytrix'></input><br>
Password: <input type='password' placeholder='Password' name='pass'></input><br>
<input type='submit' value='Login' name='submit'> </input><br><br>
<?php
echo $_POST['user'];
?>
</body>
</html>
Error: Undefined index: user<br>
It doesn't matter how i try to get the value from an input in will never get it. So my question is what am i doing wrong or why does it not work?
Upvotes: 1
Views: 169
Reputation: 43
<?php if (isset($_POST['submit'])) {
echo $_POST['user'];
}
else
{
?>
<form name="loginForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Username: <input type='text' placeholder='Username' name='user' id='user' value='Nytrix'></input><br>
Password: <input type='password' placeholder='Password' name='pass'></input><br>
<input type='submit' value='Login' name='submit'> </input><br><br>
<?php
}
?>
Upvotes: 2
Reputation: 58
You need <form action="" method="post"> ... Inputs ... </form>
. Then you have a form to submit.
Upvotes: 0
Reputation: 21532
for $_POST
to exist, the current page must have posted. You don't post any data to this page. A way to achieve this is to add a form to your page, in which you can add your elements and submit button.
example (w3schools)
<form name="input" action="demo_form_action.asp" method="post">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>
ps: the input elements don't admin closing tags.
Upvotes: 0
Reputation: 1681
Try something like this:
<?php
if ( isset( $_POST['submit'] ) ) {
echo $_POST['user'];
}
?>
Upvotes: 0
Reputation: 1707
You need to submit the form in order to have access to data inside $_POST
. In your example you are accessing $_POST
content without making sure there is a for submission. Once you click the submit button, the $_POST
code will work.
Upvotes: 0