Jump to content

[SOLVED] how-to realtime username check during registration?


acctman

Recommended Posts

OK I wrote some very basic AJAX and php for this post. I hope it will get your started.

 

for this code to work you need to have a mysql table named user with two fields. first field is 'id' and second one is userID. I am giving you the sql to create the table and also insert some basic user data

 


CREATE TABLE `users` (
  `id` int(11) NOT NULL auto_increment,
  `userID` varchar(20) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;


INSERT INTO `users` (`id`, `userID`) VALUES 
(1, 'Tanvir'),
(2, 'john'),
(3, 'kelly'),
(4, 'smith'),
(5, 'jason');


 

You need to create two files.

 

1. the html form named form.html

2. the php file name userCheck.php. it will actually check in the mysql data base to check for existing user.

 

I am first posting the HTML file first

<html>
<head>
<title>
Real Time User Name Check

</title>

<script type="text/javascript">
/*

some codes are taken from book named "Head Rush AJAX" by head first lab

*/

var request=null;
function createRequest()
{
try{

request= new XMLHttpRequest();

}catch(tryMicrosft){
try{
request= new ActiveXObject("Msxml2.XMLHTTP");

}catch(otherMicrosoft){
try{

request=new ActiveXObject("Microsoft.XMLHTTP");
}catch(failed){

request=null;
}
}
}
if(request==null)
alert("Error creating request object!");
//else alert("Successfully created");
}

function checkUser()
{





var userID=document.getElementById("email").value;

createRequest();
var url= "userCheck.php?userID="+userID;
//alert(url);

request.open("GET",url,true);
request.onreadystatechange=updatePage;
request.send(null);
}
function updatePage()
{

if(request.readyState==4)
replaceText('user',request.responseText);
}










function clearText(el) {
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        el.removeChild(childNode);
      }
    }
  }
}


<!--
function replaceText(el, text) {
  if (el != null) {
    el=document.getElementById(el);
    clearText(el);
   //setTimeout("alert("HELLO")", 4000);
    
    
    var newNode = document.createTextNode(text);
    
   el.appendChild(newNode);
   
   

  
  }
}



//-->
</script>

</head>

<body>


<form action='userCheck.php'  method='get' >

<table>
  <tr>
  
   <td>
   user ID
   </td>
    <td>
<input  type="text" id="email" name="userID" onblur="checkUser()">    

    
    </td>
    <td><div id=user >   </div> </td>
  </tr>
  
  <tr>
  <td>
  user Name
  </td>
  <td><input type="text" name="userName"></td>
</table>


<input type=submit >

</form>
</body>
</html>

And now goes the php file named userCheck.php

 

<?php
$user='root';//Give you database user name
$pass='pinki';//give your pass
$serverName='localhost';//give you server name

$dbName='test';//give your database name

$userNameToCheck=htmlentities($_GET['userID']);// Getting the GET value and convert html 
$connect=mysql_connect($serverName,$user,$pass);//connecting to the DB


if(!$connect)// Connection test
die('Unable to Connect'.mysql_error($connect));

$db=mysql_select_db($dbName);//Selecting the DB

if(!db)
die('Unable to select Database'.mysql_error($connect));


$query="select userID from users where userID='$userNameToCheck'"; //Querying the DB for the user ID given by the user

$resource=mysql_query($query,$connect);//Querying the DB


if(!$resource)die('Unable to query'.mysql_error($connect));

if(mysql_num_rows($resource)>0)//This is the main logic. If user is already in the DB, than the function will return more than 0
echo 'This user ID is already taken,Please choose another one';
else echo 'User ID ok';

?>

 

I hope it helps. If you have any further question, please let me know

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.