Login MySQL, PHP and AJAX

2

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.

    
asked by Kiku S. 15.05.2017 в 10:23
source

2 answers

0

I have already solved my doubt. In case someone has the same problem.

PHP     

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{
        function statustrue(){
            return true;
        }
        statustrue();
        echo json_encode(statustrue());
    }
}else{
    function statusfalse(){
        return false;
    }
    statusfalse();
    echo json_encode(statusfalse());
}   
?>

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();
    //localStorage.clear();
    $.ajax({
        data: data_login,
        type: "POST",
        url: "http://urlservidor.com/validate_user.php"
    })
    .done(function(data){
        //alert ("Done " + email + password);
        //alert(data);
        if(data == "true"){
            //alert("true");
            localStorage.setItem("Email", JSON.stringify(email));
            window.location.replace("../index.html");
        } else{
            //alert("false");
            window.location.replace("login.html");
        }
    })
    return false;
});
});
    
answered by 15.05.2017 / 18:12
source
2

AJAX

.fail () is if the ajax query fails. This means: errors in php, there is no file (404), error 500, etc.

.done () means that you have run the php without errors.

You have the following faults:

  • You are not making an alert of the answer, you are doing an alert of the javascript variables that you have declared above
  • In php you are returning up to 4 values to the ajax query:

     }else{
     return $email;
     return $pas;
     echo $email;
     echo $pas;}
    

You have to return only one value, or an object to return multiple values.

A simple example of ajax is this:

$.ajax({
 method: "POST",
 url: "some.php",
 data: { name: "John", location: "Boston" }
})
 .done(function( msg ) {
   alert( "Data Saved: " + msg );
});

They are rookie mistakes. Look at the AJAX documentation well and disguise it a bit: link

    
answered by 15.05.2017 в 10:47