check username

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Mar 2008
Posts: 145
Reputation: queenc is an unknown quantity at this point 
Solved Threads: 4
queenc's Avatar
queenc queenc is offline Offline
Junior Poster

check username

 
0
  #1
May 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 1
Reputation: amudhan.rkr is an unknown quantity at this point 
Solved Threads: 0
amudhan.rkr amudhan.rkr is offline Offline
Newbie Poster

Re: check username

 
1
  #2
Jul 1st, 2008
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...


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Untitled Document</title>
  6. <script type="text/javascript" language="javascript">
  7. <!--
  8. // Function returns a XML Request Object
  9. function getXMLReqObject()
  10. {
  11. var objReq = null;
  12. // branch for native XMLHttpRequest object
  13.   if (window.XMLHttpRequest)
  14. {
  15.   objReq = new XMLHttpRequest();
  16.   }
  17. // branch for IE/Windows ActiveX version
  18. else if (window.ActiveXObject)
  19. {
  20.   objReq = new ActiveXObject("Microsoft.XMLHTTP");
  21.   }
  22. return objReq;
  23. }
  24.  
  25. var httpReq = null;
  26. var frmObj = document.frmAdd;
  27. function validateForm()
  28. {
  29. if(!chkText(frmObj.txtUserName,'User Name field is empty. Please enter value'))
  30. return false;
  31.  
  32. var httpReq = getXMLReqObject();
  33. if(httpReq)
  34. {
  35. var url = 'ajax.php?userName='+frmObj.txtUserName.value+'&userOpt=0';
  36.  
  37. httpReq.open("GET", url, false);
  38. httpReq.send(null);
  39. if (httpReq.readyState == 4)
  40. {
  41. if (httpReq.status == 200)
  42. {
  43. if(httpReq.responseText == 'Yes')
  44. {
  45.  
  46. alert('User Name already exist! Please try with any other User Name');
  47. return false;
  48. }
  49. }
  50. else
  51. {
  52. alert("There was a problem retrieving data:\n" + httpReq.statusText);
  53. return;
  54. }
  55. }
  56.  
  57. }
  58. }
  59. //-->
  60. </script>
  61. </head>
  62. <body>
  63. <div>
  64. <table width="95%" border=0 align="center" cellpadding="0" cellspacing="0">
  65. <tr>
  66. <td width="100%" align="middle">
  67. <form method="post" name="frmAdd" id="frmAdd" action="{$smarty.server.PHP_SELF}?Opt=addUser" onSubmit="return validateForm();">
  68. <table width="100%" border="0" align="center" cellpadding="3" cellspacing="0">
  69. <tr>
  70. <td width="14%" valign="top">&nbsp;</td>
  71. <td width="1%" valign="top">&nbsp;</td>
  72. <td width="85%" align="left" valign="top">* represents mandatory </td>
  73. </tr>
  74. <tr>
  75. <td align="right" valign="top" nowrap="nowrap">User Name * </td>
  76. <td valign="top">:</td>
  77. <td align="left" valign="top"><input name="txtUserName" type="text" id="txtUserName" size="30" /></td>
  78. </tr>
  79. <tr>
  80. <td valign="top">&nbsp;</td>
  81. <td valign="top">&nbsp;</td>
  82. <td align="left" valign="top">&nbsp;</td>
  83. </tr>
  84. <tr>
  85. <td>&nbsp;</td>
  86. <td valign="top">&nbsp;</td>
  87. <td align="left"><input name="btnAddUser" type="submit" id="btnAddUser" value="Add User" />&nbsp;&nbsp;&nbsp;&nbsp;</td>
  88. </tr>
  89. </table>
  90. </form></td>
  91. </tr>
  92. <tr>
  93. <td align="middle"><img src="../images/spacer.gif" height="1" width="1" border="0"></td>
  94. </tr>
  95. </table>
  96. </div>
  97. </body>
  98. </html>

SERVER SIDE SCRIPTING FILE (PHP)
  1. <?php
  2.  
  3. //**** Function to check whether the User Name already exist or not ****//
  4. function chkUserName($userName, $option = 0, $orgUserName)
  5. {
  6.  
  7. mysql_connect("localhost","user_name","password");
  8. mysql_select_db("database_name");
  9.  
  10. $sql = "SELECT * FROM users WHERE UCASE(user_name) = '".strtoupper($userName)."'";
  11.  
  12. //Option for Updating
  13. if($option == 1)
  14. $sql .= " AND UCASE(user_name) != '".strtoupper($orgUserName)."' ";
  15.  
  16. $result=mysql_query($sql);
  17. if($result)
  18. {
  19. if(mysql_num_rows($result) > 0)
  20. return 1;
  21. }
  22. return 0;
  23. }
  24.  
  25.  
  26. if($_GET)
  27. {
  28. //**** Checking User Name ****//
  29. if($_GET['userName'])
  30. {
  31. $retVal = 'No';
  32. if(chkUserName($_GET['userName'],$_GET['userOpt'],$_GET['orgUserName']))
  33. $retVal = 'Yes';
  34. echo $retVal;
  35. exit();
  36. }
  37.  
  38. }
  39.  
  40.  
  41. ?>

Have a nice day...

Regards,
Amudhan R
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC