In the following code, I want the username to be checked if it's available or not before submiting the form. I used onchange() event with the username field.
HTML Code:
<form method="post" action="RegConf.php">
<p>
<input type="text" id="username" name="username" value="" placeholder="Username" onchange="check_ava()">
<span id='ava_result'>
</p>
<p>
<input type="password" id="password" name="password" value="" placeholder="Password">
</p>
<p>
<input type="password" id="confPassword" name="confPassword" value="" placeholder="Confirm Password" onchange="check()">
<span id='message'>
</p>
<p>
<input type="text" name="email" value="" placeholder="Email">
</p>
<p>
<input type="text" name="phone" value="" placeholder="Phone">
</p>
<p>
<input type="text" name="address" value="" placeholder="Address">
</p>
<p class="submit">
<input type="submit" id="submit" name="commit" value="Submit">
<button type="reset" value="Reset">Reset</button>
</p>
</form>
function check_ava() {
var username = document.getElementById('username').value;
$.post("check_username.php", { username: username }, function(result){
if (result == 1) {
document.getElementById('ava_result').innerHTML = "Username is available";
document.getElementById('submit').disabled = false;
} else {
document.getElementById('ava_result').innerHTML = "Username is not available";
document.getElementById('submit').disabled = true;
}
});
}
<?php
include 'DBConnection.php';
$username = mysql_real_escape_string($_POST['username']);
$result = mysql_query('select Name from User where Name = "'. $username .'"');
if (mysql_num_rows($result) > 0) {
echo 0;
} else {
echo 1;
}
?>
The problem is that it is not working and not checking the username.