Page 1 of 1

Check availability of user name in database

Posted: Wed Jul 09, 2008 12:06 am
by Swathimaddipatla
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....

Re: Check availability of user name in database

Posted: Wed Jul 09, 2008 1:55 am
by sureshmaharana
Try This One
<html>
<html>
<head>
<script language="JavaScript" type="text/javascript">

var xmlHttp
function check_user()
{
var username = document.getElementById('Username').value;
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" value="" />
<input type="button" name="check" id="check" value="Check Availability" onclick="return check_user()"/></td>
<div id="txtHint" style="font-size: 11px;font-weight: bold;color:#FF3300"> </div>
</tr>
</html>




AJAX.PHP

<?php
$con = mysql_connect("localhost","root","");
if(!$con)
{
die("could not connect:".mysql_error());
}
mysql_select_db("dbname",$con);
$username = $_GET['c'];
$count = mysql_num_rows(mysql_query("select * from tablename where username = '".$username."'"));

if($count > 0) {
echo 'That username already exists, please select another one.';
}else{
echo 'That username is available.';
}

?>

Re: Check availability of user name in database

Posted: Wed Jul 09, 2008 2:41 am
by Swathimaddipatla
Thank u suresh..now its working

Re: Check availability of user name in database

Posted: Tue Mar 02, 2010 7:25 pm
by tedah3143
How would I configure this to accomodate logging into my PHP MySQL Admin 2.11 on a remote server? Primarily the Username and Login...what would that look like?