KeyUp Function in Jquery on id.
<script type="text/javascript" src="jquery-1.9.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
$("#docid").keyup(function (e) {
//removes spaces from username
$(this).val($(this).val().replace(/\s/g, ''));
var username = $(this).val();
//if(username.length < 4){$("#user-result").html('');return;}
//if(username.length >= 4){
$("#user-result").html('<img src="ajax-loader.gif" />');
$.post('check_username.php', {'username':username}, function(data) {
$("#user-result").html(data);
});
//}
});
});
</script>
<table>
<tr>
<td class="text">DocID</td>
<td><input type="text" name="docid" class="text1" id="docid"/><span id="user-result"></span></div></td>
</tr>
check_username.php
<?php
###### db ##########
$db_username = 'root';
$db_password = 'root';
$db_name = 'doctors';
$db_host = 'localhost:3305';
################
//check we have username post var
if(isset($_POST["username"]))
{
//try connect to db
$connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
//trim and lowercase username
$username=$_POST['username'];
//sanitize username
//check username in db
$results = mysqli_query($connecDB,"SELECT DocID FROM doctors_detail WHERE DocID='$username'");
//return total count
$username_exist = mysqli_num_rows($results); //total records
//if value is more than 0, username is not available
if($username_exist) {
die('<img src="not-available.png" />');
}else{
die('<img src="available.png" />');
}
//close db connection
mysqli_close($connecDB);
}
?>
<script type="text/javascript">
$(document).ready(function(e) {
$("#docid").keyup(function (e) {
//removes spaces from username
$(this).val($(this).val().replace(/\s/g, ''));
var username = $(this).val();
//if(username.length < 4){$("#user-result").html('');return;}
//if(username.length >= 4){
$("#user-result").html('<img src="ajax-loader.gif" />');
$.post('check_username.php', {'username':username}, function(data) {
$("#user-result").html(data);
});
//}
});
});
</script>
<table>
<tr>
<td class="text">DocID</td>
<td><input type="text" name="docid" class="text1" id="docid"/><span id="user-result"></span></div></td>
</tr>
check_username.php
<?php
###### db ##########
$db_username = 'root';
$db_password = 'root';
$db_name = 'doctors';
$db_host = 'localhost:3305';
################
//check we have username post var
if(isset($_POST["username"]))
{
//try connect to db
$connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
//trim and lowercase username
$username=$_POST['username'];
//sanitize username
//check username in db
$results = mysqli_query($connecDB,"SELECT DocID FROM doctors_detail WHERE DocID='$username'");
//return total count
$username_exist = mysqli_num_rows($results); //total records
//if value is more than 0, username is not available
if($username_exist) {
die('<img src="not-available.png" />');
}else{
die('<img src="available.png" />');
}
//close db connection
mysqli_close($connecDB);
}
?>