我想做的是使用ajax和pdo w/按钮作为提交者创建一个登录系统。我的问题是当我单击按钮执行时没有任何反应。控制台中没有错误。我在网络中看到它会将用户名和密码数据发送到 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>
用户.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";
}
}
}
?>
连接.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
而不是纯文本。如果您需要对此进行澄清,请询问:-)
关于javascript - 我的登录系统中的 user.php 不会响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24075676/