gpt4 book ai didi

php - 如何通过 jquery keyup 检查数据库中是否存在用户名

转载 作者:行者123 更新时间:2023-11-30 00:01:21 25 4
gpt4 key购买 nike

嗨,我正在创建一个包含用户名密码等的表单。我使用 jquery 来验证该表单,我想检查该用户名是否已存在于 mysql 数据库中。如果存在,则不应提交表单。

这是迄今为止我的代码:

<form id="register" name="register" action="somewhere.php" method="post" >
<label for ="Username"> Username </label><br>
<input type="text" class="register-control" id="Username" name="Username" autocomplete="off" placeholder="Enter Username"> <br>
<div class="username_avail_result" id="username_avail_result"></div><br>
<label for ="Password"> Password </label><br>
<input type="password" class="register-control" id="password" name="password" placeholder="Enter Password" ><br><br>
<label for ="Confirm-Password"> Confirm Password </label><br>
<input type="password" class="register-control" id="Confirm_Password" name="Confirm_Password" placeholder="Confirm Password" ><br>
<label for="email" > Email </label><br>
<input type ="email" class="register-control" id="email" name="email" placeholder="Enter Valid Email"><br><br>
<button type="submit" >Submit</button>

</form>

jquery 是:

<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#Username').keyup(function(){ // Keyup function for check the user action in input
var Username = $(this).val(); // Get the username textbox using $(this) or you can use directly $('#username')
var UsernameAvailResult = $('#username_avail_result'); // Get the ID of the result where we gonna display the results
if(Username.length > 2) { // check if greater than 2 (minimum 3)
UsernameAvailResult.html('Loading..'); // Preloader, use can use loading animation here
var UrlToPass = 'action=username_availability&username='+Username;
$.ajax({ // Send the username val to another checker.php using Ajax in POST menthod
type : 'POST',
data : UrlToPass,
url : 'checker.php',
success: function(responseText){ // Get the result and asign to each cases
if(responseText == 0){
UsernameAvailResult.html('<span class="success">Number available</span>');
}
else if(responseText > 0){
UsernameAvailResult.html('<span class="error">Number already taken</span>');
}
else{
alert('Problem with sql query');
}
}
});
}
});



$("#register").validate({


errorElement: 'div',
rules:{
"Username":{
required: true,
minlength:5
},
"password":{
required:true,
minlength:5
},
"Confirm_Password":{
required:true,
equalTo: "#password"
},
"email":{
required:true,
}
},
messages: {
Username:{
required: "Please provide a username",
minlength: "Your Username must be at least 5 characters long"
},
password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long"
},
Confirm_Password: {
required: "Please provide a confirm password",
equalTo: "Please enter the same password as above"
}
},
email:{
required: "Please provide a valid email",
}
});
});
</script>

两者都工作得很好,我的疑问是如何合并两者,而且如果用户名已被使用,则不应提交表单。

最佳答案

在您的 php 文件中运行查询,获取当前电子邮件地址(即“example@example.com”)和用户名(即“example”)的所有用户(如果有) rows 那么它存在,因此不要添加它,否则,添加它!

    $_GET['email'] = $email;
$result = mysql_query("SELECT * FROM users WHERE email='$email'");
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
if(mysql_num_rows($result) >= 1) {
//show already registered
//you can either return false or change the return string/array to "registered"
} else {
mysql_query('INSERT INTO users ..............);
}

关于php - 如何通过 jquery keyup 检查数据库中是否存在用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25013311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com