hi
i am very new to ajax,please tell me how to check whether username already present in the db.i want the user name to be unique please tell me the code to check.
thanks.

Hi,
I have given you the sample code developed to to check User Name. I have done it using HTML and PHP. If you have any query feel free to contact... :)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" language="javascript">
<!--
// Function returns a XML Request Object
function getXMLReqObject() 
{    
	var objReq = null;
	// branch for native XMLHttpRequest object
    if (window.XMLHttpRequest)
	{
        objReq = new XMLHttpRequest();        
    } 
	// branch for IE/Windows ActiveX version
	else if (window.ActiveXObject)
	{
        objReq = new ActiveXObject("Microsoft.XMLHTTP");        
    }
	return objReq;
}

var httpReq = null;
var frmObj = document.frmAdd;
function validateForm()
{
	if(!chkText(frmObj.txtUserName,'User Name field is empty. Please enter value'))
		return false;
		
	var httpReq = getXMLReqObject();
	if(httpReq)
	{
		var url = 'ajax.php?userName='+frmObj.txtUserName.value+'&userOpt=0';
			
		httpReq.open("GET", url, false);
		httpReq.send(null);		
		if (httpReq.readyState == 4)
		{
			if (httpReq.status == 200)
			{				
				if(httpReq.responseText == 'Yes')
				{							
					
					alert('User Name already exist! Please try with any other User Name');
					return false;
				}				
			} 
			else 
			{
				alert("There was a problem retrieving data:\n" + httpReq.statusText);
				return;
			}
		}							
			
	}
}
//-->
</script>
</head>
<body>
<div> 
<table  width="95%"  border=0 align="center" cellpadding="0" cellspacing="0">
  <tr> 
    <td width="100%" align="middle">
	<form method="post" name="frmAdd" id="frmAdd" action="{$smarty.server.PHP_SELF}?Opt=addUser" onSubmit="return validateForm();">
	  <table width="100%" border="0" align="center" cellpadding="3" cellspacing="0">	   
  <tr>
    <td width="14%" valign="top">&nbsp;</td>
    <td width="1%" valign="top">&nbsp;</td>
    <td width="85%" align="left" valign="top">* represents mandatory </td>
  </tr>
  <tr>
    <td align="right" valign="top" nowrap="nowrap">User Name * </td>
    <td valign="top">:</td>
    <td align="left" valign="top"><input name="txtUserName" type="text" id="txtUserName" size="30" /></td>
  </tr>
  <tr>
    <td valign="top">&nbsp;</td>
    <td valign="top">&nbsp;</td>
    <td align="left" valign="top">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td valign="top">&nbsp;</td>
    <td align="left"><input name="btnAddUser" type="submit" id="btnAddUser" value="Add User" />&nbsp;&nbsp;&nbsp;&nbsp;</td>
  </tr>
      </table>
	</form></td>
  </tr>
  <tr>
    <td align="middle"><img src="../images/spacer.gif" height="1" width="1" border="0"></td>
  </tr>
</table>
</div>
</body>
</html>

SERVER SIDE SCRIPTING FILE (PHP)

<?php

//**** Function to check whether the User Name already exist or not ****//
function chkUserName($userName, $option = 0, $orgUserName)
{
	
	mysql_connect("localhost","user_name","password");
	mysql_select_db("database_name");

	$sql = "SELECT * FROM users WHERE UCASE(user_name) = '".strtoupper($userName)."'";

	//Option for Updating
	if($option == 1)
	$sql .= " AND UCASE(user_name) != '".strtoupper($orgUserName)."' ";

	$result=mysql_query($sql);
	if($result)
	{
		if(mysql_num_rows($result) > 0)
			return 1;
	}
	return 0;
}


if($_GET)
{
	//**** Checking User Name ****//
	if($_GET['userName'])
	{
		$retVal = 'No';		
		if(chkUserName($_GET['userName'],$_GET['userOpt'],$_GET['orgUserName']))
			$retVal = 'Yes';
		echo $retVal;
		exit();		
	}
		
}


?>

Have a nice day... :)

Regards,
Amudhan R

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.