dp9599 2014-06-06 06:36
浏览 64
已采纳

我的登录系统中的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";
        }  
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站