title: ajax - check if a username exists + return message if it does
body: <p>im trying to check if a username a user wants is already taken without having to send the form, basically onBlur of the username field.</p>
  
  <p>I'm having some trouble and have a few questions.</p>
  
  <p>I have my input field plus js:</p>
  
  <pre><code>&lt;script src="js/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  $(document).ready(function(){
  
  $('#username').on('blur', checkdb);
  
  function checkdb(){ 
      var desiredUsername = $(this).val();
      $.ajaxSetup({
          url: "check-db.php",
          type: "POST",
      });
  
      $.ajax({
          data: 'desiredUsername='+desiredUsername,       
          success: function (msg) {
              alert (msg);},
              error: function (XMLHttpRequest, textStatus, errorThrown){   
              alert('Error submitting request.'); 
          }   
      });     
  }           
  });
  &lt;/script&gt;
  
  &lt;input type="text" name="username" id="username"&gt;
  </code></pre>
  
  <p>and my check-db.php file:</p>
  
  <pre><code>$mysqli = mysqli_connect("localhost","connection info");
  if ($mysqli-&gt;connect_errno) {
  echo "Failed to connect to MySQL: (" . $mysqli-&gt;connect_errno . ") " . $mysqli-&gt;connect_error;
  }
  
  $desiredUsername = $_POST['desiredUsername'];
  
  
  $stmt = $mysqli-&gt;prepare("SELECT * FROM users WHERE username = '?' ");
  $stmt-&gt;bind_param('s', $desiredUsername);
  $stmt-&gt;execute();
  </code></pre>
  
  <p>firstly: im getting an error from my php code, 'number of variables doesnt match number of parameters in prepared statement', which im a bit confused about? I have 1 variable and 1 peramater, don't i?</p>
  
  <p>then, once this is actually working, how do I send a message/ variable back to the input page? so if the username is taken I can say so, or if its available say so?</p>
  
  <p>thanks!</p>