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

My user.php in my login system wont response

本文关键字:我的 响应 系统 登录 user php      更新时间:2023-09-26

我想做的是使一个登录系统使用ajax和pdo/按钮作为提交者。我的问题是,当我点击按钮执行什么都没有发生。控制台中没有错误。我在网络中看到它将用户名和密码数据发送给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', '');
    }
}
?>

正如你在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而不是纯文本。如果你需要澄清,就问:-)

你所做的是调用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";
    }  
?>