我正在使用下面的 PHP 和 jQuery 脚本,我正在尝试检查数据库中是否不存在通过的用户名。
PHP
if(mysqli_query($con, "SELECT id FROM user WHERE user_name='$username'")){
echo '1'; //If there is a record match in the Database - Not Available
} else {
echo '0'; //No Record Found - Username is available
}
}
?>
jQuery
$(document).ready(function(){
$("fieldset input[name=username]").change(function(){
var username = $("fieldset input[name=username]").val();//Get the value in the username textbox
if(username.length > 3){
$(".username_status").html('<img src="loader.gif" align="absmiddle"> Checking availability...');
$.ajax({
type: "POST",
url: "/donald/settings/check-username.php", //file name
data: "username="+ username, //data
success: function(data){
console.log("server_response: "+data);
if(data == '0'){
$(".username_status").html('<font color="Green"> Available </font>');
} else if(data == '1'){
$(".username_status").html('<font color="red">Not Available </font>');
}
}
});
} else {
$(".username_status").html('<font color="#cc0000">Username too short</font>');
}
return false;
});
});
结果总是通过1