使用ajax和pdo的单页登录系统

时间:2014-06-02 16:14:04

标签: javascript php jquery html ajax

我想做的是使用ajax和pdo创建一个没有页面刷新的登录系统。因为我打算做一个单页网站,人们可以登录并使用show和hide javascript转到他的个人资料。

我的问题是当我按下登录按钮时没有任何反应。

的index.php

<?php include_once('myIncludes.php'); ?>
<html lang="en">
<head>
<script src="bootstrap/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
   $('#login').click(function (e) {
       e.preventDefault();

       var emptyFieldsUser = $('#user').filter(function () { return this.value === '' });
       var emptyFieldsPass = $('#pass').filter(function () { return this.value === '' });
       if (emptyFieldsUser.length) && (emptyFieldsPass.length) {
           alert("try again");
       } else {
           var data = {};
           data.user_text = $('#user').val();
           data.pass_text = $('#pass').val();

           $.ajax({
               type: "POST",
               url: "user.php",
               data: data,
               cache: false,
               success: function (response) {
                   alert("login sccessfully!");
               }
            });
        }
    });
});
</script>
</head>
<body>
    <form method="post">    
        Username: <input type="text" name="user" id="user" /><br />
        Password: <input type="text" name="pass" id="pass" /><br />
        <button type="submit" name="login" id="login">Login</Button>
    </form>
</body>
</html>

myIncludes.php

<?php 
session_start();
include_once('connection.php');
include_once('user.php');
if(isset($_POST['submit'])){
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    $object = new User();
    $object->Login($user, $pass);
}
?>

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', '');
    }
}
?>

1 个答案:

答案 0 :(得分:0)

您的javascript中存在语法错误:

if (emptyFieldsUser.length) && (emptyFieldsPass.length) {
                          ^ The if is closed here

你可能想要OR代替AND,否定条件:

if (!emptyFieldsUser.length || !emptyFieldsPass.length) {

在你的php中你也有一些问题:

  • 您没有发送提交按钮,以便检查您应该检查您要发送的两个值之一的帖子,或使用if ($_SERVER['REQUEST_METHOD'] === 'POST');
  • 您的帖子变量将是$_POST['user_text'];$_POST['pass_text'];,因为这是您在ajax功能中设置键的内容。

另请注意,当您到达javascript成功函数时,这并不意味着登录成功。它只表示php脚本成功运行。你应该使用你得到的回复。