I'm using the below PHP and jQuery script and I'm trying to check that the username put through doesn't exist in the database already.
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;
});
});
The result always comes through as 1