title: Check username availability ajax
body: <p>As the title says, I am trying to check if a username exists in the DB in a registration form using Ajax.</p>
  
  <p>I use the following script:</p>
  
  <pre><code>    $(document).ready(function() {
  
      //the min chars for username
      var min_chars = 3;
  
      //result texts
      var characters_error = 'Minimum amount of chars is 3';
      var checking_html = 'Checking...';
  
      //when button is clicked
      $('#user_id').keyup(function(){
          //run the character number check
          if($('#user_id').val().length &lt; min_chars){
              //if it's bellow the minimum show characters_error text '
              $('#username_availability_result').html(characters_error);
          }else{
              //else show the cheking_text and run the function to check
              $('#username_availability_result').html(checking_html);
              check_availability();
          }
      });
  
  });
  
  //function to check username availability
  function check_availability(){
  
      //get the username
      var user_id = $('#user_id').val();
  
      //use ajax to run the check
      $.post("db.php", { user_id: user_id },
          function(result){
              //if the result is 1
              if(result == 1){
                  //show that the username is available
                  $('#username_availability_result').html(user_id + ' is Available');
              }else{
                  //show that the username is NOT available
                  $('#username_availability_result').html(user_id + ' is not Available');
              }
          });
  
  }
  </code></pre>
  
  <p>and my <em>db.php</em> file looks like this:</p>
  
  <pre><code>if (isset($_POST['action']) &amp;&amp; $_POST['action'] == "signup") {
  $user_id      = mysql_real_escape_string($_POST["user_id"]);
  $new_password = md5(mysql_real_escape_string($_POST["new_password"]));
  $new_email    = mysql_real_escape_string($_POST["new_email"]);
  $checking_existance=mysqli_query($str,"SELECT username FROM users WHERE username='".$user_id."'");
  $row=mysqli_fetch_assoc($checking_existance);
  print_r($checking_existance);
  if(mysqli_num_rows($checking_existance)&gt;0)
   {
      echo 0;
   }
  else{
      echo 1;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The problem I am encountering is that the result is always showing that the <em>username</em> is not available even if it is. </p>