title: Check returned data from ajax/php
body: <p>I did an update with <a href="https://stackoverflow.com/questions/20549709/ajax-request-returns-10-responses-per-second">all things you told me</a>, this is my result.
  COMPLETE UPDATE:</p>
  
  <pre><code>$(document).ready(function(){
    $("#submit").submit(function(e){
  
  e.preventDefault();
  
  var username = $("#username").val();
  var result;
  var request;
  
  if(username){
      request = $.ajax({
          url: 'check.php',
          data: {data: JSON.stringify(username)},
          type: 'POST',
          dataType: "json",
          success: function (data) {
              result = data;
          }
      });
  } else {
      alertify.error( "ERROR" );
  }
  
  request.done(function() {
      console.log(result);
      if(request.result == 1) {
          alert("yes")
      } else {
          alert("no")
      }
  });
  });
  });
  </code></pre>
  
  <p>check.php:</p>
  
  <pre><code>$usernameChecker = new UsernameChecker($config);
  
  $data = $_POST['data'];
  $data = json_decode($data,true);
  
  if(!empty($data)) {
      if ($usernameChecker-&gt;check_regex($data)) {
          if($usernameChecker-&gt;check_length($data)) {
              if (!$usernameChecker-&gt;check($data)) {
                  echo json_encode(array("error" =&gt; "Username already taken" , "result" =&gt; 0));
              } else {
                  echo json_encode(array("error" =&gt; "Username available" , "result" =&gt; 1));
              }
          } else {
              echo json_encode(array("error" =&gt; "Username too long" , "result" =&gt; 0));
          }
      } else {
          echo json_encode(array("error" =&gt; "Allowed symbols: a-z , A-Z , 1-9 and \"_\"" , "result" =&gt; 0));
      }
  } else { 
      echo json_encode(array("error" =&gt; "You forgot to type your username" , "result" =&gt; 0));
  }
  </code></pre>
  
  <p>This is what I have now, just console says:</p>
  
  <blockquote>
    <p>"result is not defined"</p>
  </blockquote>
  
  <p>SO:</p>
  
  <p>When I submit empty input, I get request is undefined.</p>
  
  <p>When I submit filled input, I get alert "no". Moreover this part:</p>
  
  <blockquote>
    <p>"Username available" </p>
  </blockquote>
  
  <p>is marked red.    </p>
  
  <p>But data.result is "1" in console?</p>