Basically, I am trying do my login form in a way that it says "Incorrect username or password" on the same page then proceed if it is correct. I have tried looking into other threads relating to this but I have not been able to understand what their code usually means. This is my code.
<div id="login">
<h1>Welcome Back!</h1>
<form method="POST" action="http://localhost:3000/api/login" id="loginform">
<div class="field-wrap">
<label>
Username<span class="req">*</span>
</label>
<input type="userID" id="userID" name= "userID" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Password<span class="req">*</span>
</label>
<input type="Password" id="Password" name="Password" required autocomplete="off"/>
</div>
<button class="button button-block"><input type="submit" id="submit" value="Log In"</button>
</form>
As for my jquery code,
$(document).ready(function(){
// Set form variable
var form = $('#loginform');
form.submit(function(event){
var userid = $('#userID').val();
var password = $('#Password').val();
if ( $.trim(userid) != '' && $.trim(password) != '') {
// Process AJAX request
$.post('http://localhost:3000/api/login',
{username: userid, Password: password }, function(data){
console.log(data);
});
}
// Prevent default form action
event.preventDefault();
});
});
I want to be able to output "Incorrect username or password" if the data entered is wrong and remain on the same page and proceed if correct but due to action="http://localhost:3000/api/login"
which will redirect to another page even if it is wrong however I also need this action="..."
to call my api to execute the checking of id and password. Any help is appreciated and do link the thread if you think it might help in terms of my problem. Thanks!
Some links I have looked over but don't understand:
Submit form and stay on same page?
jQuery : submit and remain in same page how to stay on the same page after clicking submit button