Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Checking for Unique Usernames

Options
  • 25-06-2008 8:17pm
    #1
    Registered Users Posts: 9,225 ✭✭✭


    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 Posts: 9,225 ✭✭✭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