0

I am having trouble to get my code working. As a user types in a username it checks the database to see if it exists and if it does sets the <P id="checkusername"> to "Username Taken." and if not sets it to "Not Taken." I can't see why my code is not working.

1-register.html

<html>
  <body>
    <CENTER>
      <form name="register" action="register.php" method="post">
        Username: <input type="text" name="username" onkeyup="checkUsername(this.value)" />
        <P id="checkusername">checker</P> 
        <input type="submit" value="Login" />      
      </form>
    </CENTER>

    <script type="text/javascript">
      function checkUsername(){
        var xmlhttp;
        var username=document.forms["register"]["username"].value;
        if(username.length==0){
          document.getElementById("checkusername").innerHTML="Empty";
          return;
        }
        if(window.XMLHttpRequest){
          xmlhttp=new XMLHttpRequest();
        }else{
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        var url = "login.php";
        var params = "header=checkusername&username="+username+"&password="";
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length);
        xmlhttp.setRequestHeader("Connection", "close");

        xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
          document.getElementById("checkusername").innerHTML=xmlhttp.responseText;
    }
      }
      xmlhttp.send(params);
    }
    </script>
  </body>
</html>

2-register.php

<?php
  $header = $_POST["header"];
  if(header=="checkusername"){
    checkusername($_POST["username"]);
  }else{
    echo "No match: " . $header;
  }

  function connection(){
    $con = mysql_connect(URL, username, password);
    if(!$con){
      die('Could not connect: ' . mysql_error());
    }
    return $con;
  }
  function checkusername($username){
    $con = connection();
    mysql_select_db(database, $con);
    $result = mysql_query("SELECT * FROM users WHERE username = \"" . $username . "\";");
    while($row = mysql_fetch_array($result)){
      if(($row['username'] == $username)){
        echo "Username Taken.<br/>";
        return;
      }
    }
    echo "Not Taken.";
  }
  mysql_close();
?>
jprofitt
  • 10,874
  • 4
  • 36
  • 46
sbnarra
  • 556
  • 4
  • 9
  • 25
  • 1
    restrict your question to smaller code examples. And only the relevant parts. – Itay Moav -Malimovka May 07 '12 at 12:20
  • in your while loop in php file, you are always outputting "Not taken", it should be under with IF condition.. plus.. why you need to loop to see if username matches.. your sql already did that.. just see if count is 0 its not taken.else its taken – Kamal May 07 '12 at 12:21
  • 1
    @pom - if you are not willing to expand on that it doesn't really help anyone... – Lix May 07 '12 at 12:28
  • Don't use
    it's deprecated!
    – skywalker Dec 23 '15 at 09:56

1 Answers1

3

Read this Blog http://papermashup.com/jquery-php-mysql-username-availability-checker/

Dilip Godhani
  • 2,065
  • 3
  • 18
  • 33