acctman Posted July 30, 2007 Share Posted July 30, 2007 anyone have an idea on how to query a username table while a user is inputting in a text box? i'd like for the user to check to see if the user name is valid Quote Link to comment https://forums.phpfreaks.com/topic/62476-solved-how-to-realtime-username-check-during-registration/ Share on other sites More sharing options...
thedarkwinter Posted July 30, 2007 Share Posted July 30, 2007 you cant do it with php as its server-end. you will have to submit the value in order to check it. if there is another solution, i would say its using java (not js), but i dont know. Quote Link to comment https://forums.phpfreaks.com/topic/62476-solved-how-to-realtime-username-check-during-registration/#findComment-310909 Share on other sites More sharing options...
akitchin Posted July 30, 2007 Share Posted July 30, 2007 you could use AJAX for this, which will send a server request asynchronously (ie. using a javascript onblur event to tell PHP to check the current value against the database). Quote Link to comment https://forums.phpfreaks.com/topic/62476-solved-how-to-realtime-username-check-during-registration/#findComment-310914 Share on other sites More sharing options...
Fadion Posted July 30, 2007 Share Posted July 30, 2007 Maybe u can do it with a list of usernames in xml and some ajax, but my knowledge of javascript is a disaster. Quote Link to comment https://forums.phpfreaks.com/topic/62476-solved-how-to-realtime-username-check-during-registration/#findComment-310920 Share on other sites More sharing options...
envexlabs Posted July 30, 2007 Share Posted July 30, 2007 akitchin said it best. If you have a basic knowledge of AJAX it shouldn't be to hard to complete. Quote Link to comment https://forums.phpfreaks.com/topic/62476-solved-how-to-realtime-username-check-during-registration/#findComment-310922 Share on other sites More sharing options...
ViN86 Posted July 30, 2007 Share Posted July 30, 2007 you could use AJAX for this, which will send a server request asynchronously (ie. using a javascript onblur event to tell PHP to check the current value against the database). this is how to do it Quote Link to comment https://forums.phpfreaks.com/topic/62476-solved-how-to-realtime-username-check-during-registration/#findComment-310937 Share on other sites More sharing options...
acctman Posted July 30, 2007 Author Share Posted July 30, 2007 thanks, i'll do some research on how to do it with ajax Quote Link to comment https://forums.phpfreaks.com/topic/62476-solved-how-to-realtime-username-check-during-registration/#findComment-311185 Share on other sites More sharing options...
cooldude832 Posted July 30, 2007 Share Posted July 30, 2007 don't fully rely on it because if a user doesn't have js enabled make sure your registration double checks to make sure that name isn't used Quote Link to comment https://forums.phpfreaks.com/topic/62476-solved-how-to-realtime-username-check-during-registration/#findComment-311188 Share on other sites More sharing options...
tanvir Posted July 31, 2007 Share Posted July 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62476-solved-how-to-realtime-username-check-during-registration/#findComment-311504 Share on other sites More sharing options...
acctman Posted July 31, 2007 Author Share Posted July 31, 2007 thanks tanvir Quote Link to comment https://forums.phpfreaks.com/topic/62476-solved-how-to-realtime-username-check-during-registration/#findComment-311560 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.