Hi,
I'm new to JQuery and attempting to write a script to check username availability. My problem is that no matter what I type, I always get back "This username is already in use."
JQuery source:
$(document).ready(function() {
jQuery.validator.addMethod("usernameCheck", function(username) {
var isSuccess = false;
$.ajax({ url: "username_availability.php",
data: "username=" + username,
async: false,
success:
function(msg) { isSuccess = msg === "TRUE" ? true : false }
});
return isSuccess;
},"");
$("#register_form").validate({
onkeyup:false,
rules: {
username: {
required: true,
minlength: 3,
usernameCheck: true // remote check for duplicate username
}
},
messages: {
username: {
required: "username is required.",
minlength: jQuery.format("username must be at least {0} characters in length."),
usernameCheck: "This username is already in use."
}
}
});
PHP Source:
include('database_connection.php');
if (isset($_POST['username'])) {
$username = mysql_real_escape_string($_POST['username']);
$check_for_username = mysql_query("SELECT user_id FROM users WHERE username='$username'");
if (mysql_num_rows($check_for_username)) {
echo "TRUE";
} else {
echo "FALSE"; //No Record Found - Username is available
}
}
?>
HTML Source:
<input class="username" id="username" type="text" name="username" value="" maxlength="20" />
Thanks a lot