I am developing an application with Phonegap . I would like that when I log in, I will check if it exists in the database. In case you have to save the email address ( email
) in localStorage
.
HTML Code:
<div class="login_container">
<form id="login-form" method="POST" name="login_form">
<input class="input_email_login" type="email" placeholder="Email" name="email" required>
<input class="input_password_login" type="password" placeholder="Password" name="password" required>
<p>Forgot your password?</p><span>Click here</span>
<button id="submit_login" type="submit">Login</button>
</form>
</div>
PHP code (hosted on the server):
<?php
$email = $_POST["email"];
$pas = $_POST["password"];
require_once("../connect.php");
$db = new Conexion();
$dbTable='r_users';
$consult = "SELECT COUNT(*) FROM $dbTable WHERE email=:log AND password=:pas";
$result = $db->prepare($consult);
$result->execute(array(":log" => $email, ":pas" => md5($pas)));
$total = $result->fetchColumn();
if($total==1){
$consult = "SELECT * FROM $dbTable WHERE email=:log AND password=:pas";
$result = $db->prepare($consult);
$result->execute(array(":log" => $email, ":pas" => md5($pas)));
if (!$result) {
print "<p>Error.</p>\n";
}else{
return $email;
return $pas;
echo $email;
echo $pas;
}
}else{
return false;
}?>
Code jQuery :
$(function() {
$('#submit_login').on('click', function() {
var data_login = $("#login-form").serialize();
var email = $('.input_email_login').val();
var password = $(".input_password_login").val();
$.ajax({
data: data_login,
type: "POST",
url: "http://urlservidor.com/validate_user.php"
})
.done(function(){
alert ("Done " + email + password);
localStorage.setItem("Email", JSON.stringify(email));
window.location.replace("../index.html");
})
.fail(function(){
alert ("No");
window.location.replace("../login.html");
})
return false;
});
});
The fact is that when trying to log in I always get Done
, it shows me correctly the email address and the password that I entered. Whether the user is correct or false.