PHP,MySql User Availability Check
In This tutorial we are going to discuss about how to write code for user availability check dynamically from database. the working principle is if we type any word in a text field it will show the result like if it available it give Available otherwise Already in use.
index.php
<div class="content">
<input type="url" name="url" id="url">
<span id="status"></span>
</div>
<input type="url" name="url" id="url">
<span id="status"></span>
</div>
connection.php
<?php
$con= mysql_connect("localhost","root","user_availability");
if(!$con)
{
die('could not connect;'.mysql_error());
}
mysql_select_db("demo",$con);
?>
$con= mysql_connect("localhost","root","user_availability");
if(!$con)
{
die('could not connect;'.mysql_error());
}
mysql_select_db("demo",$con);
?>
Ajax.js
<?php
include('connect.php');
if(isset($_POST['url']))
{
$url = $_POST['url'];
$sql = mysql_query("select id from Registration where url='$url'");
if(mysql_num_rows($sql))
{
echo '<strong>'.$url.'</strong> is already in use.';
}
else
{
echo 'OK';
}
}
?>
include('connect.php');
if(isset($_POST['url']))
{
$url = $_POST['url'];
$sql = mysql_query("select id from Registration where url='$url'");
if(mysql_num_rows($sql))
{
echo '<strong>'.$url.'</strong> is already in use.';
}
else
{
echo 'OK';
}
}
?>
Script.js
<script type="text/javascript">
$(document).ready(function()
{
$("#url").change(function()
{
var url = $("#url").val();
var msgbox = $("#status");
if(url.length > 4)
{
$("#status").html('<img src="loader.gif" align="middle"> Checking availability...');
$.ajax({
type: "POST",
url: "ajax.php",
data: "url="+ url,
success: function(msg){
$("#status").ajaxComplete(function(event, request){
if(msg == 'OK')
{
$("#url").removeClass("red");
$("#url").addClass("green");
msgbox.html('<img src="images/available.png" align="absmiddle">');
}
else
{
$("#url").removeClass("green");
$("#url").addClass("red");
msgbox.html(msg);
}
});
}
});
}
else
{
$("#url").addClass("red");
$("#status").html('<font color="#cc0000">Please nter atleast 5 letters</font>');
}
return false;
});
});
</script>
$(document).ready(function()
{
$("#url").change(function()
{
var url = $("#url").val();
var msgbox = $("#status");
if(url.length > 4)
{
$("#status").html('<img src="loader.gif" align="middle"> Checking availability...');
$.ajax({
type: "POST",
url: "ajax.php",
data: "url="+ url,
success: function(msg){
$("#status").ajaxComplete(function(event, request){
if(msg == 'OK')
{
$("#url").removeClass("red");
$("#url").addClass("green");
msgbox.html('<img src="images/available.png" align="absmiddle">');
}
else
{
$("#url").removeClass("green");
$("#url").addClass("red");
msgbox.html(msg);
}
});
}
});
}
else
{
$("#url").addClass("red");
$("#status").html('<font color="#cc0000">Please nter atleast 5 letters</font>');
}
return false;
});
});
</script>
No comments:
Post a Comment