检查用户名和密码是否匹配或可用-jQuery [英] Checking if Username and Password is match or available - Jquery
问题描述
我的代码有问题,用户名和密码不匹配. 这是我的输出.如果用户名和密码不匹配,则会提示不匹配;但是,如果用户名和密码匹配,则会提示信息不匹配. 这是下面的代码:
I'm having a problem on my code which the username and password does not match. Here is my output. When the username and password is not match, then it will give a message that it is not match , However, when the username and password match, then there will be a message that it is match here is my code below:
html代码
<body>
<div class="container box">
<div class="form-group">
<h3 align="center">Live Username Available or not By using PHP Ajax Jquery</h3><br />
<label>Enter Username</label>
<input type="text" name="username" id="username" class="form-control" />
<input type="password" name="password" id="password" class="form-control" />
<span id="availability"></span>
<br /><br />
<button type="button" name="register" class="btn btn-info" id="register" disabled>Register</button>
<br />
</div>
<br />
<br />
</div>
</body>
</html>
脚本代码
<script>
$(document).ready(function(){
$('#username','#password').blur(function(){
var username = $(this).val();
var password = $(this).val();
$.ajax({
url:'check.php',
method:"POST",
data:{user_name:username, password:password},
success:function(data)
{
if(data != '1')
{
$('#availability').html('<span class="text-danger">Username and Password not Match</span>');
$('#register').attr("disabled", true);
}
else
{
$('#availability').html('<span class="text-success">Username and Password Available</span>');
$('#register').attr("disabled", false);
}
}
})
});
});
</script>
check.php -我的数据库连接和查询,该查询是从数据库中获取的
check.php - my database connection and query that fetch it from the database
<?php
//check.php
$connect = mysqli_connect("localhost", "username", "", "dbname");
if(isset($_POST["user_name"] && $_POST["password"]))
{
$username = mysqli_real_escape_string($connect, $_POST["user_name"]);
$password = mysqli_real_escape_string($connect, $_POST["password"]);
$query = "SELECT * FROM admin WHERE username = '".$username."' AND password = '".$password."' ";
$result = mysqli_query($connect, $query);
echo mysqli_num_rows($result);
}
?>
推荐答案
注释
- 您广泛接受SQL注入.您应该改用准备好的语句.请阅读如何防止PHP中的SQL注入?
- 您不应以纯文本形式存储密码.而是使用PHP的
password_hash
和password_verify
函数进行哈希和验证.
- You are wide open to SQL injection. You should be using prepared statements instead. Please read How can I prevent SQL injection in PHP?
- You should not store passwords in plain text. Instead, use PHP's
password_hash
andpassword_verify
functions to hash and verify.
答案
您使用的 isset
错误.要检查是否设置了多个值,请用逗号分隔它们,而不用&&
:
if(isset($_POST["user_name"], $_POST["password"]))
您当前使用的PHP代码不会产生任何输出,因为它将在该行上以致命错误终止.
Your PHP code as it currently stands won't produce any output as it will terminate with a fatal error on that line.
在jQuery中,您没有正确指定多个选择器.它们都应放在同一组引号内
In your jQuery, you're not specifying multiple selectors correctly. They should all be inside the same set of quotes:
$('#username, #password').blur(function(){
您还需要更改此代码,这会将username
和password
的值设置为相同的内容:
You also need to change this code, which will set both username
and password
values to the same thing:
var username = $(this).val();
var password = $(this).val();
到
var username = $('#username').val();
var password = $('#password').val();
这篇关于检查用户名和密码是否匹配或可用-jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!