Using jQuery to check username availability...
So I posted a question on here and I'm pretty new to jQuery... I can't figure out exactly how to do this. I'm trying to, when a field blurs, run a query to see if the username is in the database, and echo whether the username is available or not if it's in the database. I have a little snipet of my page and I just don't see how to specify how to tell the script which input field, when it gets blured, to run the query... here is what I have...
<html>
<head>
<script type=\"text/javascript\">
$('#new_user').blur(function() {
var newName = $(this).val();
$.post('inc/check_name.php', {
new_user: newName
}, function(data){
var usernameCount = data;
if(1 == usernameCount){
$('#new_user').next('.error').css('display', 'inline');
} else {
$('#new_user').next('.error').css('display', 'none');
}
}, 'html');
});
</script>
</head>
<body>
<form action="register.php" method="POST">
Username: <input type="text" name="regUsername" value="">
</form>
<?php
$checkUser = "SELECT `username` ";
$checkUser .= "FROM `database`.`table` ";
$checkUser .= "WHERE `username` = '".$_POST['new_user']."' ";
if(!($check = mysql_query($checkUser, $dbc))){
echo mysql_errno();
exit();
}
$userCount = mysql_num_rows($check);
echo $userCount;
?>
</body>
</html>
I'm pretty good with PHP so I'm sure I can setup the file check_name.php to connect to the database and search for the username, It's just getting the jQuery to work properly :(
If anyone could show me how to put that together and get it running, I would appreciate it!
Thanks in advance