jQuery Validate issue with username checking with database

jQuery Validate issue with username checking with database

I got this code from the internet and it work perfectly for the email but with username there is a problem. When ever i type something it display the error 'Username have been taken' even with spam. I just wonder if anyone can fix it up. Thanks.

My Script

  1. $(document).ready(function () {
  2. $('.registerForm').validate({
  3.     onkeyup: false,
  4.     rules: {
  5.         username: {
  6.             required: true,
  7.             minlength: 5,
  8.             maxlength: 20,
  9.             remote: {
  10.             url: 'core/check2.php',
  11.         type: "post",
  12.         data: 
  13.         {
  14.           username: function() {
  15.             return $( "#username" ).val();
  16.             }
  17.             }
  18.             }
  19.         },
  20.         password: {
  21.             required: true,
  22.             minlength: 5,
  23.             maxlength: 20
  24.         },
  25.         confirmpassword: {
  26. required: true,
  27. minlength: 5,
  28. maxlength: 20,
  29. equalTo: password
  30.         },
  31.         email: {
  32.             required: true,
  33.             remote: {
  34.             url: 'core/check.php',
  35.         type: "post",
  36.         data: {
  37.           email: function() {
  38.             return $( "#email" ).val();
  39.             }
  40.             }
  41.             }
  42.         },
  43.         code: {
  44.             required: true,
  45.             remote: {
  46.             url: 'core/checkcode.php',
  47.         type: "post",
  48.         data: {
  49.           code: function() {
  50.             return $( "#code" ).val();
  51.             }
  52.             }
  53.             }
  54.         }
  55.     },
  56.     messages: {
  57.         username: {
  58.             required: "Enter a username.",
  59.             minlength: "Username too short",
  60.             maxlength: "Username too long",
  61.             remote: "Username have been taken"
  62.         },
  63.         password: {
  64.             required: "Enter a password.",
  65.             minlength: "Password too short",
  66.             maxlength: "Password too long",
  67.             remote: "Email have been taken"
  68.         },
  69.         confirmpassword: {
  70.             required: "Confirm your password.",
  71. minlength: "Password too short",
  72.             maxlength: "Password too long",
  73.             equalTo: "Your password does not match"
  74.         },
  75.         email: {
  76.             required: "Enter your email(NOT SPAM EMAIL).",
  77.             remote: "This Email have been taken"
  78.         },
  79.         code: {
  80.             required: "Enter your Register Code",
  81.             remote: "Invalid Code"
  82.         }
  83.     }

  84. });
  85. });






This is the php for username.
  1.  
  2. <?php
  3. include_once("db_connect.php");
  4. // USERNAME
  5. if(!empty($_POST['username'])) {
  6.     $username = $mysqli->real_escape_string($_POST['username']);
  7.     $query = "SELECT * FROM users WHERE username = '$username'";
  8.     $results = $mysqli->query($query);
  9.     if($results->num_rows == 0)
  10.     {
  11.         echo "true";  //good to register
  12.     }
  13.     else
  14.     {
  15.         echo "false"; //already registered
  16.     }
  17. } else {
  18.     echo "false";
  19. }
  20. ?>