javascript 使用 PHP 和 JQuery 的登录表单验证 MySQL 数据库中的登录信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19718876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Login form using PHP and JQuery to validate login information from MySQL database
提问by Rob
I have a login form that uses JQuery and PHP to validate a username and password. I am trying to switch over from extension mysql to mysqli for better practice and am having a lot of trouble. I think the error exists in the JQuery somewhere because I've tested the login.php validation and it works fine.
我有一个使用 JQuery 和 PHP 来验证用户名和密码的登录表单。我正在尝试从扩展 mysql 切换到 mysqli 以获得更好的练习,但遇到了很多麻烦。我认为错误存在于 JQuery 的某处,因为我已经测试了 login.php 验证并且它工作正常。
So here is my code:
所以这是我的代码:
index.php:
索引.php:
<!doctype html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#login_a").click(function(){
$("#shadow").fadeIn("normal");
$("#login_form").fadeIn("normal");
$("#user_name").focus();
});
$("#cancel_hide").click(function(){
$("#login_form").fadeOut("normal");
$("#shadow").fadeOut();
});
$("#login").click(function(){
var username=$('#user_name').val();
var password=$('#password').val();
$.ajax({
type: "POST",
url: "login.php",
data: "name="+username+"&pwd="+password,
success: function(html){
if(html=='true'){
$("#login_form").fadeOut("normal");
$("#shadow").fadeOut();
$("#profile").html("<a href='logout.php' id='logout'>Logout</a>");
}
else{
$("#add_err").html("*Wrong username or password");
}
},
beforeSend:function(){
$("#add_err").html("Loading...")
}
});
return false;
});
});
</script>
</head>
<body>
<?php session_start(); ?>
<div id="profile">
<?php if(isset($_SESSION['user_name'])){ ?>
<a href='logout_script_2.php' id='logout'>Logout</a>
<?php }else {?>
<a id="login_a" href="#">login</a>
<?php } ?>
</div>
<div id="login_form">
<form action="login_script_2.php" method="POST">
<label>User Name:</label>
<input type="text" id="user_name" name="user_name" />
<label>Password:</label>
<input type="password" id="password" name="password" />
<label></label><br/>
<input type="submit" id="login" value="Login" />
<input type="button" id="cancel_hide" value="Cancel" />
</form>
<div class="err" id="add_err"><br></div>
</div>
<div id="shadow" class="popup"></div>
</body>
</html>
login.php
登录.php
<?php
session_start();
$con = mysqli_connect("localhost","root","PW","db") or die("Connection error: " . mysqli_error($con));
if(isset($_POST['submit'])){
$username = $_POST['name'];
$password = $_POST['pwd'];
$stmt = $con->prepare("SELECT * FROM tapp_login WHERE username = ? AND password = ? LIMIT 1");
$stmt->bind_param('ss', $username, $password);
$stmt->execute();
$stmt->bind_result($username, $password);
$stmt->store_result();
if($stmt->num_rows == 1)
{
if($stmt->fetch())
{
$_SESSION['Logged'] = 1;
$_SESSION['user_name'] = $username;
echo 'Access granted';
exit();
}
}
else {
echo "*Wrong username or password";
}
$stmt->close();
}
else {
}
$con->close();
?>
logout.php
登出.php
<?php
session_start();
unset($_SESSION['user_name']);
header('Location: index.php');
?>
All the attempts to run the code give me the error in the JQuery validation "*Wrong username or password". Thanks in advanced!
运行代码的所有尝试都给了我 JQuery 验证中的错误“*错误的用户名或密码”。先谢谢了!
回答by Sean
When logging in using ajax, you are not posting submit
, so this line
使用ajax登录时,你没有发帖submit
,所以这一行
if(isset($_POST['submit'])){
is never true so your code never executes.
永远不会是真的,所以你的代码永远不会执行。
Either change it to
要么将其更改为
if(isset($_POST['name'])){
or add it to your ajax posted data
或将其添加到您的 ajax 发布数据中
data: "submit=true&name="+username+"&pwd="+password,
回答by StackSlave
AJAX is a partial submission. Make sure you're firing it without using a submit button, or return false;
inside your click function, at the bottom. I would use <input type='button' id='login_a' />
, if it's not a link. Additionally, you are not setting your submit button, like @Sean said, because it's AJAX.
AJAX 是部分提交。确保在不使用提交按钮的情况下触发它,或者return false;
在底部的点击功能内。<input type='button' id='login_a' />
如果它不是链接,我会使用。此外,您没有像@Sean 所说的那样设置提交按钮,因为它是 AJAX。