dp9599
2014-06-06 06:36
浏览 61

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

I want to do is make a login system using ajax and pdo w/ button as submiter. My problem is when i click the button to execute nothing happens. There is no error in the console. I see in the network its sends the username and password data to user.php but after that nothing realy happens in the 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条回答 默认 最新

  • duanjiani6826 2014-06-06 08:04
    已采纳

    What you are doing is calling user.php, but inside user.php you just have a class with a login function, so what you're basicly doing is calling a .php file that does not know what to do with the username and password that has just been posted with AJAX, but what you want is calling the Login function.

    What you could do is create a new user.php and put this in:

    <?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";
        }  
    ?>
    
    点赞 评论
  • dongxiao1591 2014-06-06 06:40

    As you can see in your 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";
            }
        }
    }
    ?>
    

    You aren't instantiating your class anywhere.... Which means that no processing is being done.

    From what I can see; you'd want to do something like:

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

    And it would probably be best to return json instead of plain text. If you need clarification on that, just ask :-)

    点赞 评论

相关推荐 更多相似问题