0

I'm working on a simple login in form that verfies one username and one password using php and javascript. I have a index.php a javascript file to check for errors and a results.php. I can't see anything that I'm doing wrong, but the page continues to submit Invalid login in no matter what is input. Looking for some hints if you guys can give me some. Thanks in advance. I have also looked at similar questions on here and my code pretty much matches the others.

results.php

<?php

$user="joesmith";
$password="abc123";
if(isset($_POST['submit'])){

if(($_POST['user'] == $user) && ($_POST['password'] == $password))
echo "You have successfully logged in";
}
else
{
echo "Invalid login! Try again.";
}
?>

index.php

<body>

<form class="basic-grey" action="results.php" method="post">

<div>
<label for="username" id="lUsernameLabel">* Username:</label>
<input type="text" name="username" id="iUsername"      onblur="validateUserName()" onfocus="resetUserName()" />
<div id="dUsernameError" class="errorMessage"></div>
</div>

<div>
<label for="password" id="lPasswordLabel">* Password:</label>
<input type="password" name="password" id="iPassword"  onblur="validatePassword()" onfocus="resetPassword()" />
<div id="dPasswordError" class="errorMessage"></div>
</div>

<div>
<input type="submit" class="button" value="Submit" />
</div>
4
  • 1
    You forgot to give your submit button the name "submit". Commented Feb 13, 2015 at 22:01
  • Don't rely on JavaScript for input validation, checks must be done server-side, e.g. in PHP. Commented Feb 13, 2015 at 22:02
  • You can use your browser's developer tools to check what the actual outgoing HTTP request looks like. That would have saved you some time here. Commented Feb 13, 2015 at 22:02
  • check input validation on both client and server side @Mex Commented Feb 13, 2015 at 22:03

3 Answers 3

6

your input has name="username" but you are looking for $_POST['user']

Sign up to request clarification or add additional context in comments.

7 Comments

Bull's eye! He found it!
I'm confused, when the users puts a username in and hits submit is it not Posting that information? Or are you saying that the input needs to reflect $_POST as well?
What I mean is your <input> element that is taking the username entered, is named 'username' in the html file, so it will be posted at $_POST['username'] to the php file, whereas you are looking for $_POST['user'] which doesn't exist.
Oh okay I got you, though even changing it didn't make it succeed. On the results.php I changed if(($_POST['user'] ==$user) to if(($_POST['username'] == $user).
Then I would suggest echo'ing out the two posted values just before they are compared.
|
0

You are submitting username input with "username" but trying to check its validity with "user".

Change the part

($_POST['user'] == $user)

to

($_POST['username'] == $user)

it will start to work

Comments

0

Try $_POST['username'] instead of $_POST['user'].

(This would be a comment if I had the rep)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.