Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Checking for Unique Usernames

  • 25-06-2008 08:17PM
    #1
    Registered Users, Registered Users 2 Posts: 9,228 ✭✭✭


    right so i have written a registration form which validates fine using JS, I just added a username field and want to ensure the username is unique. This is how i attempted to do it:

    Javascript
    if (window.xmlhttprequest) {
            xmlhttp = new xmlhttprequest();
        } 
        
        else if (window.ActiveXObject) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    
        xmlhttp.open('GET', 'test.php?username='+username, false);
        xmlhttp.send('');
            
       if (xmlhttp.status == 200) {
           return xmlhttp.responseText.indexOf('true') == -1;
       } else return false; }
    

    PHP
    <?php
        include ('../includes/connectionManagerClass.php');
        $openDb=new MYSQLDB;
        if (isset($_REQUEST['username']) && !empty($_REQUEST['username'])) {
            $username=$_REQUEST['username'];
            $query="SELECT * FROM USERS WHERE USERNAME = '".$username."'";
            $result=mysql_query($query);
            
            if(mysql_num_rows($result)==0){
                echo("true");
            }
            
            else {
                echo("false");
            }
            
        } else {
            echo("false");
        }
    ?>
    

    since i added in the JS above to my validation class it now doesnt validate at all and i can register with none of the fields filled out. The php file definitely works.

    anyone have any suggestions as to what i am doing wrong, i really dont know much about the xmlhttp stuff so it could be horribly wrong.

    any help would be great!


Comments

  • Registered Users, Registered Users 2 Posts: 9,228 ✭✭✭Chardee MacDennis


    not sure what i did wrong but i got it figured, here's the working code in case it comes in use for someone!
    xmlhttp=null;
        if (window.XMLHttpRequest) {// all modern browsers
          xmlhttp=new XMLHttpRequest();
        }
        
        else if (window.ActiveXObject) {// for IE5, IE6
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
         
        if (xmlhttp!=null) {
              xmlhttp.open('GET', 'test.php?username='+username, false);
              xmlhttp.send(null);
        }
        
          if (xmlhttp.status==200) {
            if(xmlhttp.responseText.indexOf('true') == -1) {
                document.myform.Name.focus();
                showError("errorRegister","That username is already in use, please choose another",0,0);
                return false;
            }
        }
    


Advertisement