Check availability of user name in database
Posted: Wed Jul 09, 2008 12:06 am
Hi..
am trying to check whether user name entered by user is available or not.
for that i need to check database.
By using on change function i can do that.but am trying to put button with value Check.On click of button it has to check with database and result has to be display in the DIV area.
this is my form.php code
<html>
<html>
<head>
<script language="JavaScript" type="text/javascript">
var username = document.getElemetbyId('check');
var xmlHttp
function check_user(username)
{
alert(username);
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="ajax.php"
url=url+"?c="+username
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
<tr>
<td width="117" ><b>Username</b></td>
<td width="14">:</td>
<td width="357"><input name="Username" type="text" id="Username" size="40" />
<input type="button" name="check" id="check" value="Check Availability" onclick="return check_user(document.getElemetbyId('Username'))"/></td>
<div id="txtHint" style="font-size: 11px;font-weight: bold;color:#FF3300"> </div>
</tr>
</html>
and my ajax.php code is
<?php
$con = mysql_connect("localhost","root","root123");
if(!$con)
{
die("could not connect:".mysql_error());
}
mysql_select_db("testtool",$con);
$username = $_GET[$c];
$count = mysql_num_rows(mysql_query("select * from `details_user` where `username`='".$username."'"));
header('Content-Type: text/xml');
header('Pragma: no-cache');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<result>';
if($count > 0) {
echo 'That username already exists, please select another one.';
}else{
echo 'That username is available.';
}
echo '</result>';
?>
please check y this function is not working....
am trying to check whether user name entered by user is available or not.
for that i need to check database.
By using on change function i can do that.but am trying to put button with value Check.On click of button it has to check with database and result has to be display in the DIV area.
this is my form.php code
<html>
<html>
<head>
<script language="JavaScript" type="text/javascript">
var username = document.getElemetbyId('check');
var xmlHttp
function check_user(username)
{
alert(username);
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="ajax.php"
url=url+"?c="+username
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
<tr>
<td width="117" ><b>Username</b></td>
<td width="14">:</td>
<td width="357"><input name="Username" type="text" id="Username" size="40" />
<input type="button" name="check" id="check" value="Check Availability" onclick="return check_user(document.getElemetbyId('Username'))"/></td>
<div id="txtHint" style="font-size: 11px;font-weight: bold;color:#FF3300"> </div>
</tr>
</html>
and my ajax.php code is
<?php
$con = mysql_connect("localhost","root","root123");
if(!$con)
{
die("could not connect:".mysql_error());
}
mysql_select_db("testtool",$con);
$username = $_GET[$c];
$count = mysql_num_rows(mysql_query("select * from `details_user` where `username`='".$username."'"));
header('Content-Type: text/xml');
header('Pragma: no-cache');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<result>';
if($count > 0) {
echo 'That username already exists, please select another one.';
}else{
echo 'That username is available.';
}
echo '</result>';
?>
please check y this function is not working....