简体   繁体   English

即使详细信息正确,Ajax登录也会返回错误的详细信息

[英]Ajax login returning incorrect details even when details are correct

I am new to ajax, jQuery and JavaScript and this is more of a learning exercise for myself to brush up on some skills. 我是ajax,jQuery和JavaScript的新手,这对我自己来说是一种学习一些技能的学习练习。 I want to move away from login pages that are separate pages (ie loginForm.php that sends data to login.php which then redirects to a logged in section) and want to move down a jQuery ajax route. 我想离开单独页面的登录页面(即,将数据发送到login.php的loginForm.php,然后将其重定向到已登录的部分),并希望下移jQuery ajax路由。

I have built my index page as follows: 我建立了索引页面,如下所示:

<form id="login_form">
    <h1>Login Form</h1>
    <div>
        <input type="text" placeholder="Username" required="" id="user_name"  name="user_name"/>
    </div>
    <div>
        <input type="password" placeholder="Password" required="" id="password"  name="password"/>
    </div>
    <div class="err" id="add_err"></div>
    <div>
        <input type="submit" value="Log in" id="login"  />
    </div>
</form> 

I have set my JavaScript up like so: 我已经像这样设置了JavaScript:

$("#login").click(function(){
    username=$("#user_name").val();
    password=$("#password").val();
    console.log(username + " " + password);
    var info = {username:username, password:password};
    console.log(info);
    var sub = $.ajax({
        async: false,
        type: "POST",
        url: "login.php",
        data: info,
        success: function(html){
        if(html=='true')
        {
            $("#login_form").fadeOut("normal");
        }
        else
        {
            $("#add_err").html("Wrong username or password");
        }
    },
    beforeSend:function()
    {
         $("#add_err").html("Loading...")
    },
    error: function(response) { console.log(response); }
    });
    console.log(sub);
    return false;
});

when I console.log(info) it tells me in the log that this is an object and it has the information that I need to send. 当我console.log(info)它在日志中告诉我这是一个对象,并且包含我需要发送的信息。 So I'm sure that this part so far is okay. 因此,我确定到目前为止,这一部分还可以。 The following script is my login.php: 以下脚本是我的login.php:

<?php
include('secure.php'); // includes PDO database connection here

$username = $_POST['username'];
$password = $_POST['password'];

$sel = $db->prepare("SELECT * FROM aaUsers WHERE username='$username' AND password='$password'");
$sel->execute();
$count = $sel->rowCount();
    if ( $count >= 1) {
        echo "true";
    }
    else
    {
        echo "false";
    }
?>

The info is passed across to here and then this script runs, however, when I console.log(sub) I can see that the responseText = "" and this is causing my script to fail over. 信息传递到此处,然后此脚本运行,但是,当我console.log(sub)我可以看到responseText = "" ,这正在导致脚本故障转移。 would someone be able to inform me where I have gone wrong, I feel like I'm doing everything right, just that there seems to be somewhere I have gone wrong. 有人能够告诉我我哪里出了问题,我觉得我做对了所有事情,只是好像我哪里出了问题。

Are you using chrome, or firefox (with firebug)? 您使用的是Chrome还是Firefox(带有Firebug)? Open the developer console and watch what comes back from the AJAX call. 打开开发人员控制台,观察AJAX调用返回的内容。 If it's blank there, then as scrowler has put well, your php is faulty. 如果那里是空白的,那么正如scrowler所说的那样,您的php有问题。 You could also create a quick non-ajax testing form to post the two variables the ol' fashioned way. 您还可以创建一个快速的非ajax测试表单,以ol的方式发布两个变量。

<html>
<body><form action=login.php method=post><input name=username/> <input name=password/> <input type=submit></form></body></html>

Fill in the blanks and hit submit. 填写空白并点击提交。

My bad. 我的错。

Turns out I had called: 原来我打电话给:

$sel = $db->prepare(...);

instead of: 代替:

$sel = $conn->prepare(...);

works fine now, thank you for your suggestions for looking at my php script. 现在工作正常,谢谢您对我的php脚本的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM