title: Why is XMLHttpRequest readystate not equal to 4?
body: <p>I'm learning AJAX for the first time and I'm trying to create a simple application that calculates the grade a person needs to get on their final exam based on their previous assessment results. </p>
  
  <p>The user inputs their grades into the form and then using ajax I calculate the response in response.php which should then be output in the callback function in the div with ajax as its id. </p>
  
  <p>For some reason the xhr.readystate property is never reaching 4 so the response is never generated. Can anybody see what I'm doing wrong?</p>
  
  <p>Please bear in mind I'm only learning... </p>
  
  <p>index.php</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
    &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
      &lt;meta name="description" content=""&gt;
      &lt;meta name="author" content=""&gt;
      &lt;link rel="icon" href="../../favicon.ico"&gt;
  
      &lt;title&gt;Grade Calculator&lt;/title&gt;
  
      &lt;!-- Latest compiled and minified CSS --&gt;
      &lt;link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"&gt;
  
      &lt;link rel="stylesheet" href="style.css"&gt;
  
      &lt;!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --&gt;
      &lt;!--[if lt IE 9]&gt;
        &lt;script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"&gt;&lt;/script&gt;
        &lt;script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"&gt;&lt;/script&gt;
      &lt;![endif]--&gt;
  
      &lt;script&gt;
  
          function sendAjax() {
              var xhr = new XMLHttpRequest();
              xhr.onreadystatechange = function () {
                  if (xhr.readyState === 4) {
                      document.getElementById('ajax').innerHTML = xhr.responseText;
                  } else {
                      document.getElementById('ajax').innerHTML = document.write('There was a problem');
                  }
              };
              xhr.open("GET","response.php"); 
                  xhr.send();
                  document.getElementById('gradeForm').style.display = "none";
                  document.write(xhr.readyState);
          }
  
      &lt;/script&gt;
  
  
    &lt;/head&gt;
  
    &lt;body&gt;
  
      &lt;div class="navbar navbar-inverse navbar-fixed-top" role="navigation"&gt;
        &lt;div class="container"&gt;
          &lt;div class="navbar-header"&gt;
            &lt;button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"&gt;
              &lt;span class="sr-only"&gt;Toggle navigation&lt;/span&gt;
              &lt;span class="icon-bar"&gt;&lt;/span&gt;
              &lt;span class="icon-bar"&gt;&lt;/span&gt;
              &lt;span class="icon-bar"&gt;&lt;/span&gt;
            &lt;/button&gt;
            &lt;a class="navbar-brand" href="#"&gt;Project name&lt;/a&gt;
          &lt;/div&gt;
          &lt;div class="collapse navbar-collapse"&gt;
            &lt;ul class="nav navbar-nav"&gt;
              &lt;li class="active"&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#about"&gt;About&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#contact"&gt;Contact&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="container"&gt;
  
        &lt;div class="starter-template"&gt;
          &lt;h1&gt;Bootstrap starter template&lt;/h1&gt;&lt;br&gt;
  
          &lt;form action="index.php" method="get" id="gradeForm"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="currentGrade"&gt;What is your current Grade&lt;/label&gt;
                  &lt;input type="text" name="currentGrade"&gt;&lt;br&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="targetGrade"&gt;What is your target Grade&lt;/label&gt;
                  &lt;input type="text" name="targetGrade"&gt;&lt;br&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="finalWorth"&gt;What is your final worth?&lt;/label&gt;
                  &lt;input type="text" name="finalWorth"&gt;&lt;br&gt;
              &lt;/div&gt;
              &lt;button type="submit" class="btn btn-default" onclick="sendAjax()"&gt;Submit&lt;/button&gt;
          &lt;/form&gt;
  
          &lt;div id="ajax"&gt;
  
          &lt;/div&gt;
  
        &lt;/div&gt;
  
      &lt;/div&gt;&lt;!-- /.container --&gt;
  
  
      &lt;!-- Bootstrap core JavaScript
      ================================================== --&gt;
      &lt;!-- Placed at the end of the document so the pages load faster --&gt;
      &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
  
      &lt;!-- Latest compiled and minified JavaScript --&gt;
      &lt;script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"&gt;&lt;/script&gt;
  
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is response.php </p>
  
  <pre><code>&lt;?php 
  
          $currentGrade = $_GET['currentGrade'];
          $targetGrade = $_GET['targetGrade'];
          $finalWorth = $_GET['finalWorth'];
  
          $neededMark = $currentGrade - $targetGrade;
          $neededMark = $neededMark / $finalWorth;
          $neededMark = $neededMark * 100;
          $neededMark = round($neededMark);
  
          if(isset($currentGrade, $targetGrade,$finalWorth)) {
              echo $neededMark;
          }
  
  ?&gt;
  </code></pre>