Login with AJAX in PHP

Asked

Viewed 497 times

0

I’m studying about mobile apps, hybrid apps, I’m using phonegap and I’m having a problem. I cannot log in, ajax sends the variables right and php returns a value but ajax is not capturing this value to check whether or not the user exists in the database. Follow the html and php code:

HTML

<!DOCTYPE html>
<html>
<head>
    <link rel='stylesheet' type="text/css" href="css/style.css">
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title>Blank App</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script language=javascript type="text/javascript">
    function Login(){
        var email = document.getElementById('email').value;
        var senha = document.getElementById('senha').value;
        console.log(email);
        console.log(senha);
        var json = {'email':email, 'senha':senha};

     $.ajax({
            type:"POST", crossDomain: true, cache: false,
            url:"http://localhost/projeto/login.php",
            data:json,
            dataType:'json',
            sucess: function (data){
                console.log("logou");
                if(data=='logou'){
                    console.log("logou");
                    alert("logou");
                    window.location.href="inicial.html";
                }else{
                    alert ( " Obrigado " );
                }
            },
            error: function(e){
                console.log(e);
            }
        });
    }
    </script>
</head>
<body class='tela_login'>

    <script type="text/javascript" src="cordova.js"></script>
    <div class='form-group'>
        <form id='formulario' action=login.php method=post></form>
        <img src="/imagem/icone_login.png" class='img_login'">
        <p><input type=text class='form-login' id='email' placeholder='Insira seu email'></p>
        <p><input type=password class='form-login' id='senha' placeholder='Insira sua senha'></p>
        <button type=button class='btn-logar' onclick="Login()">Logar</button>
        <button type=submit class='btn-cadastrar'>Cadastrar</button>
        </div>
        <form>
    </div>

</body>
</html>

PHP

<?php
require "conexao.php";

$email=$_POST['email'];
$senha=$_POST['senha'];

$query=mysqli_query($conexao, "SELECT * FROM tbl_cliente where email='$email' and senha='$senha'");
$cont=mysqli_num_rows($query);
if($cont>0){
     echo "logou";
}else{ 
    echo "falhou";
}
?>

Help me out, please.

  • The best thing would be for you to send http_response_code(code); No error anything that is above 400, and in sucess other types of sponse code, if the login is not found for example, you could return 404

  • and how I would do it in the code?

1 answer

0


I believe it is because you indicate in the AJAX call that the return is of type JSON, but in PHP you do not return a JSON but a text.

To solve the problem, enter the following statement at the beginning of your PHP code:

header('Content-Type: application/json');

And on the return of your instruction, I suggest you do this:

echo json_encode(['status' => 'logou']);

We are transforming an array into JSON, with the native function json_encode().

In Javascript, you will test the return as follows:

if (data.status == 'logou') ...

In the same way in cases of failure and etc.

  • Thanks for the help but I think it’s the same phonegap problem, because I’ve tried every way and nothing... Has anyone ever used this platform to make a login system? I can’t stand to break my head because of Javascript

  • Beauty! I never worked with Phonegap, but I’ve had this problem that you reported with Ajax calls without being able to recover the return due to the types of return of the requests. I hope you can handle it. Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.