username availability gmail-style (javascript+sql)

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

Join Date: Oct 2004
Posts: 29
Reputation: trashed is an unknown quantity at this point 
Solved Threads: 0
trashed trashed is offline Offline
Light Poster

username availability gmail-style (javascript+sql)

 
0
  #1
Nov 6th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 525
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

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

 
0
  #2
Nov 6th, 2008
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
  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
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
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