我的登录系统中的user.php不会响应

时间:2014-06-06 06:36:25

标签: javascript php jquery html ajax

我想做的是使用ajax和pdo w / button作为提交者创建一个登录系统。我的问题是当我点击按钮执行没有任何反应。控制台中没有错误。我在网络中看到它将用户名和密码数据发送到user.php,但之后在index.php中没有发生任何事情。

的index.php

<html lang="en">
<head>
<script src="bootstrap/js/jquery-1.11.0.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {

   $('#myLogin').submit(function() {
        var username = $('#user').val();
        var password = $('#pass').val();


        $.ajax({
            data: {
             username : username, password : password
            },
            type: "POST",
            url: 'user.php',
            success: function(data)
            {
               $('#show').html(data);
            }
        });
            return false;
    });

});
</script>
</head>
<body>
<div id="show"></div>
<form id="myLogin"> 
Username: <input type="text" name="user" id="user" /><br />
Password: <input type="password" name="pass" id="pass" /><br />
<button type="submit" name="login" id="login">Login</Button>
</form>
</body>
</html>

user.php的

<?php
include_once('connection.php');
class User{

    private $db;

    public function __construct(){
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }
    public function Login($user, $pass){
        if(!empty($user) && !empty($pass)){
            $st = $this->db->prepare("SELECT * from users WHERE username=? AND password=?");
            $st->bindParam(1, $user);
            $st->bindParam(2, $pass);
            $st->execute();

            if($st->rowCount() == 1){
                echo "User verifies, Access granted";
            } else {
                echo "Incorrect Username or Password";
            }
        }else{
            echo "Please enter Username and Password";
        }
    }
}
?>

connection.php

<?php
class Connection{
    public function dbConnect(){
        return new PDO('mysql:host=localhost; dbname=test', 'root', '');
    }
}
?>

2 个答案:

答案 0 :(得分:2)

正如您在user.php中所看到的那样:

<?php
include_once('connection.php');
class User{

    private $db;

    public function __construct(){
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }
    public function Login($user, $pass){
        if(!empty($user) && !empty($pass)){
            $st = $this->db->prepare("SELECT * from users WHERE username=? AND password=?");
            $st->bindParam(1, $user);
            $st->bindParam(2, $pass);
            $st->execute();

            if($st->rowCount() == 1){
                echo "User verifies, Access granted";
            } else {
                echo "Incorrect Username or Password";
            }
        }else{
            echo "Please enter Username and Password";
        }
    }
}
?>

你没有在任何地方实例化你的课程....这意味着没有进行任何处理。

从我所看到的;你想做点什么:

if(!empty($_POST['username'])) {
    $user =  new User();    
    $user->Login($_POST['username'], $_POST['password']);
}

最好返回json而不是纯文本。如果您需要澄清,请问: - )

答案 1 :(得分:0)

你正在做的是调用user.php,但在user.php中你只有一个带登录功能的类,所以你基本上做的就是调用一个不知道如何处理的.php文件刚刚使用AJAX发布的用户名和密码,但您想要的是调用Login函数。

您可以做的是创建一个新的user.php并将其放入:

<?php
include_once('connection.php');

$db = new Connection();
$db = $db->dbConnect();

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

    if(!empty($user) && !empty($pass)){
        $st = $db->prepare("SELECT * from users WHERE username=? AND password=?");
        $st->bindParam(1, $user);
        $st->bindParam(2, $pass);
        $st->execute();

        if($st->rowCount() == 1){
            echo "User verifies, Access granted";
        } else {
            echo "Incorrect Username or Password";
        }
    }else{
        echo "Please enter Username and Password";
    }  
?>