Sunday, 29 September 2013

Using Javascript to run a php script to check username

Using Javascript to run a php script to check username

I've been trying to build a registeration form for a website I am
building. I can do the basics but I want it to check the username
availability without reloading the page.
JAVASCRIPT
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"
type="text/javascript" charset="utf-8"></script>
<script>
$(document).ready(function()
{
$("#Username").focusout(function()
{
//Check if usernane if available
$.post("scripts/check_username.php", {username:
$("#Username").value}, function(data)
{
if(!data)
{
$("#Username").setCustomValidity("This username is
already taken!");
}
else
{
alert('Username available');
}
});
return false;
});
});
</script>
HTML
<form id="registerForm">
<table>
<tr><td>Username</td><td><input id="Username"
class='textInput' type='text' name='username'
required></td></tr>
PHP SCRIPT
<?php
include 'open_connection.php';
$result = true;
$username = mysql_real_escape_string($_POST['username'])
$result = mysql_query("SELECT * FROM tblMembers WHERE Username='$username'")
while($row = mysql_fetch_array($result))
{
$result = false;
}
echo $result;
?>
When I leave the textbox it says username available no matter what. I
placed a username "test" in the database... no luck
Please help

No comments:

Post a Comment