943,736 Members | Top Members by Rank

Ad:
Nov 6th, 2008
0

username availability gmail-style (javascript+sql)

Expand Post »
Hi all.
I am currently developing a CMS for a school project and I wanted to add the function to check, real-time, if a username is already taken. It should be a little bit like what happens when you register for a Gmail or Yahoo! account.
Basically, while the user inputs the desired username in a textbox, I want to make appear next to it a green or red sign (plus a small text) to display availability.
The registered usernames will be in a mysql table which I can query with a very basic SELECT statement.

It should work AS THE USER TYPES, so if he's entering "mrbrown" then it should try to query for every letter.
This will be my first AJAX implementation, so I am asking for a starting point and any suggestion you deem could help me.

Thanks!
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004
Nov 6th, 2008
0

Re: username availability gmail-style (javascript+sql)

Th JavaScript code for this will be similar to: (You will need to modify this)
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. function createRequestObject() {
  3. var ro;
  4. var browser = navigator.appName;
  5. if(browser == "Microsoft Internet Explorer") {
  6. ro = new ActiveXObject("Microsoft.XMLHTTP");
  7. }else {
  8. ro = new XMLHttpRequest();
  9. }
  10. return ro;
  11. }
  12. var http = createRequestObject();
  13. function sndReq(action) {
  14. if(action.length < 3) {
  15. if(action.length == 0) {
  16. document.getElementById('usernamecheck').innerHTML = "<br />";
  17. return false;
  18. }
  19. document.getElementById('usernamecheck').innerHTML = "Username too short";
  20. return false;
  21. }else {
  22. http.open('get', 'rpc.php?action='+action);
  23. http.onreadystatechange = handleResponse;
  24. http.send(null);
  25. }
  26. }
  27. function handleResponse() {
  28. if(http.readyState == 4) {
  29. var update = http.responseText;
  30. if(update!="This name is Avaliable!") {
  31. document.getElementById('usernamecheck').innerHTML = "This name is unavaliable, please try again";
  32. }else {
  33. document.getElementById('usernamecheck').innerHTML = "<a href='Javascript:EnableDisable()'>"+update+" Click to change</a>";
  34. }
  35. }
  36. }
  37. </script>

HTML for the username box:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="text" name="un" onchange="sndReq(this.value)">

And rpc.php
php Syntax (Toggle Plain Text)
  1. <?
  2. //connect to db first
  3. $user=mysql_query("SELECT * FROM user_table WHERE un = '".$_REQUEST['action']."'")or die(mysql_error());
  4. $countrows=mysql_num_rows($user);
  5. if($countrows > 0) {
  6. echo "This name is Unavaliable";
  7. }
  8. else {
  9. echo"This name is Avaliable!";
  10. }
  11. ?>

Note, the PHP code if not good, there is no checking for bad data so it is not secure, just an example
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: I need a script or applet, to insert text into text fields in different websites.
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Where can i find the js files???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC