doutangguan2460 2014-06-02 11:07
浏览 49

如何在不刷新页面的情况下使用ajax登录系统

I want to do is make a login system without refreshing the page using a button, ajax but i dont know how to do it.

My problem is my code wont work if i click the button. How do i use ajax to communicate with my myIncludes.php if i press the button login?

index.php

<html lang="en">
<head>
<script type="text/javascript">
$('#login').click(function(){
<?php include_once('myIncludes.php'); ?>
});
</script>
</head>
<body>
<form method="post">    
Username: <input type="text" name="user" /><br />
Password: <input type="text" name="pass" /><br />
<button type="button" 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', '');
    }
}
?>
  • 写回答

3条回答 默认 最新

  • drfcaw7460 2014-06-02 11:18
    关注

    Include jQuery library to login page.

    Follow the below script to do ajax login action:

    $(document).ready(function() {
     $("#login").click(function() {
       var postData = $("form").serialize();
       jQuery.ajax({
         'url': 'myIncludes.php',
         'data': postData ,
         'type': "POST",
         'success': function(data){
           if (data == true) {
             // Logged in successfull
             alert("user loggedin successfully");
             window.location = "dashboardpage.php";
           } else {
             alert("invalid user/password");
           }
         }
       });
     });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题