[英]php/jQuery check if username exists
I have been trying to create a system that checks if the selected username is already being used or not. 我一直在尝试创建一个系统,该系统检查所选的用户名是否已被使用。 I have come up with the following code.
我想出了以下代码。
function checkUsername(username,func){
$.post("functions/checkUsername.php",{'username': username},function(data){
if(data!='1'){ popup_alert('The username you selected already exists.','Username Exists','Close'); return false; }
else{ window[func](); }
});
}
checkUsername.php returns a 0 if the username exists and 1 if it is available. 如果用户名存在,则checkUsername.php返回0;如果可用,则返回1。 I have run many tests on that.
我对此进行了许多测试。 My issue is that for some reason it is running the if statement before data is set.
我的问题是由于某种原因,它在设置数据之前正在运行if语句。 I have inserted a alert(data) before the if statement and it pops up with a 1 after the popup_alert is created.
我在if语句之前插入了一个alert(data),在popup_alert创建之后它以1弹出。
function checkUsername(username,callback){
$.post("functions/checkUsername.php",{'username': username},function(data){
callback && callback(data);
});
}
...
checkUsername(username,function(data) {
if(data!='1'){ popup_alert('The username you selected already exists.','Username Exists','Close'); return false; }
else{ window[func](); }
});
or you can use $.ajax 或者您可以使用$ .ajax
function checkUsername(username,func){
$.ajax({
type: 'POST',
async: false,
data: {'username': username},
url: 'functions/checkUsername.php',
success: function(data) {
if(data!='1'){ popup_alert('The username you selected already exists.','Username Exists','Close'); return false; }
else{ window[func](); }
},
error: function() {
alert('Connection error!');
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.