Ajax request not executed returns undefined
<p>I am designing an online multiple-choice test so the user picks one of the 3 answers(radio buttons) and then submits his/her answers. Once the submit button is pressed i need to calculate the results which i am using AJAX requests to compare each answer separately with the use of a PHP script which retrieves questions one by one as well based on the AJAX request parameter. </p>

<p>This is my AJAX request:</p>

<pre><code>function calculateResults(){
  totalPoints = 0;
  var qNum = 10;
  while(qNum != 0){
  $.ajax({
      method : "GET",
      url: "php-scripts/testing-questions.php",
      data: {queNum : qNum},
      success: function(data){
          console.log(data);
          //check if radio1 value is set
          if(document.getElementById('r1q' + qNum).check == true){
            var answer = document.getElementById('r1q' + qNum).value;
          }
          //check if radio2 value is set
          else if(document.getElementById('r2q' + qNum).check == true){
            var answer = document.getElementById('r2q' + qNum).value;
          }
          else{ //radio 3 value is set then
             var answer = document.getElementById('r3q' + qNum).value;
          }
          if(data === answer){
              totalPoints++;
          }
        }
     });
     qNum--;
  }
</code></pre>

<p>}</p>

<p>and this is my PHP script</p>

<pre><code>if(isset($_GET['queNum'])){
    $qNum = $_GET['queNum'];
    getAnswer($qNum);
      function getAnswer($num){
          $query = mysqli_query($conn, "SELECT correctAnswer
                              FROM  `Testing` 
                              WHERE  `questionNum` = '".$queNum."' ");
          $row = mysqli_fetch_array($query,MYSQLI_ASSOC);
          echo $row;
          $correA = $row['correctAnswer'];
          echo $correA;
      }
}
</code></pre>

<p>please note that the $conn variable is set from the config.php file which you are right to assume the connection with the database is working fine also the the echo statements in the function are there for debugging reasons which you should also assume the SQL query runs fine as well.</p>

<p>I have been working on this trying to find what's going wrong for more than a day, it looks simple to but i can't get it to work and no other examples here or anywhere else about AJAX exiting undefined.Anyone with more experience that could help much appreciated.</p>

<p>As requested HTML code below: </p>

<pre><code> &lt;div class="container3"&gt;
&lt;div id="startTest"&gt;
  &lt;center&gt;&lt;button class="checkButtons" onClick="startTimer()" style="width:100px;"&gt; Start Test&lt;/button&gt;&lt;/center&gt;
&lt;/div&gt;
&lt;div id="beginTest"&gt;
 &lt;input type = "text" name="timer" width="30%" id="timer" value ="10:00" readonly&gt;
&lt;/div&gt;
&lt;div id="questions"&gt;
 &lt;center&gt;
  &lt;div class="question"&gt;
    &lt;p class="q"&gt;Question 1: &lt;?php echo $question1 ?&gt;&lt;/p&gt;
    &lt;input type="radio" id="r1q1" name="answer1" value="Answer 1" checked&gt;Answer A: &lt;?php echo $answerA1 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r2q1" name="answer1" value="Answer 2"&gt;Answer B: &lt;?php echo $answerB1 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r3q1" name="answer1" value="Answer 3"&gt;Answer C: &lt;?php echo $answerC1 ?&gt; &lt;br&gt;
  &lt;/div&gt;
   &lt;div class="question"&gt;
    &lt;p class="q"&gt;Question 2: &lt;?php echo $question2 ?&gt;&lt;/p&gt;
    &lt;input type="radio" id="r1q2" name="answer2" value="Answer 1" checked&gt;Answer A: &lt;?php echo $answerA2 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r2q2" name="answer2" value="Answer 2"&gt;Answer B: &lt;?php echo $answerB2 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r3q2" name="answer2" value="Answer 3"&gt;Answer C: &lt;?php echo $answerC2 ?&gt; &lt;br&gt;
  &lt;/div&gt;
   &lt;div class="question"&gt;
    &lt;p&gt;Question 3: &lt;?php echo $question3 ?&gt;&lt;/p&gt;
    &lt;input type="radio" id="r1q3" name="answer3" value="Answer 1" checked&gt;Answer A: &lt;?php echo $answerA3 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r2q3" name="answer3" value="Answer 2"&gt;Answer B: &lt;?php echo $answerB3 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r3q3" name="answer3" value="Answer 3"&gt;Answer C: &lt;?php echo $answerC3 ?&gt; &lt;br&gt;
  &lt;/div&gt;
  &lt;div class="question"&gt;
  &lt;p class="q"&gt;Question 4: &lt;?php echo $question4 ?&gt;&lt;/p&gt;
    &lt;input type="radio" id="r1q4" name="answer4" value="Answer 1" checked&gt;Answer A: &lt;?php echo $answerA4 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r2q4" name="answer4" value="Answer 2"&gt;Answer B: &lt;?php echo $answerB4 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r3q4" name="answer4" value="Answer 3"&gt;Answer C: &lt;?php echo $answerC4 ?&gt; &lt;br&gt;
  &lt;/div&gt;
   &lt;div class="question"&gt;
    &lt;p class="q"&gt;Question 5: &lt;?php echo $question5 ?&gt;&lt;/p&gt;
    &lt;input type="radio" id="r1q5" name="answer5" value="Answer 1" checked&gt;Answer A: &lt;?php echo $answerA5 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r2q5" name="answer5" value="Answer 2"&gt;Answer B: &lt;?php echo $answerB5 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r3q5" name="answer5" value="Answer 3"&gt;Answer C: &lt;?php echo $answerC5 ?&gt; &lt;br&gt;
  &lt;/div&gt;
   &lt;div class="question"&gt;
   &lt;p class="q"&gt;Question 6: &lt;?php echo $question6 ?&gt;&lt;/p&gt;
    &lt;input type="radio" id="r1q6" name="answer6" value="Answer 1" checked&gt;Answer A: &lt;?php echo $answerA6 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r2q6" name="answer6" value="Answer 2"&gt;Answer B: &lt;?php echo $answerB6 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r3q6" name="answer6" value="Answer 3"&gt;Answer C: &lt;?php echo $answerC6 ?&gt; &lt;br&gt;
  &lt;/div&gt;
   &lt;div class="question"&gt;
    &lt;p class="q"&gt;Question 7: &lt;?php echo $question7 ?&gt;&lt;/p&gt;
    &lt;input type="radio" id="r1q7" name="answer7" value="Answer 1" checked&gt;Answer A: &lt;?php echo $answerA7 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r2q7" name="answer7" value="Answer 2"&gt;Answer B: &lt;?php echo $answerB7 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r3q7" name="answer7" value="Answer 3"&gt;Answer C: &lt;?php echo $answerC7 ?&gt; &lt;br&gt;
  &lt;/div&gt;
   &lt;div class="question"&gt;
   &lt;p class="q"&gt;Question 8: &lt;?php echo $question8 ?&gt;&lt;/p&gt;
    &lt;input type="radio" id="r1q8" name="answer8" value="Answer 1" checked&gt;Answer A: &lt;?php echo $answerA8 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r2q8" name="answer8" value="Answer 2"&gt;Answer B: &lt;?php echo $answerB8 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r3q8" name="answer8" value="Answer 3"&gt;Answer C: &lt;?php echo $answerC8 ?&gt; &lt;br&gt;
  &lt;/div&gt;
   &lt;div class="question"&gt;
   &lt;p class="q"&gt;Question 9: &lt;?php echo $question9 ?&gt;&lt;/p&gt;
    &lt;input type="radio" id="r1q9" name="answer9" value="Answer 1" checked&gt;Answer A: &lt;?php echo $answerA9 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r2q9" name="answer9" value="Answer 2"&gt;Answer B: &lt;?php echo $answerB9 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r3q9" name="answer9" value="Answer 3"&gt;Answer C: &lt;?php echo $answerC9 ?&gt; &lt;br&gt;
  &lt;/div&gt;
   &lt;div class="question"&gt;
    &lt;p class="q"&gt;Question 10: &lt;?php echo $question10 ?&gt;&lt;/p&gt;
    &lt;input type="radio" id="r1q10" name="answer10" value="Answer 1" checked&gt;Answer A: &lt;?php echo $answerA10 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r2q10" name="answer10" value="Answer 2"&gt;Answer B: &lt;?php echo $answerB10 ?&gt; &lt;br&gt;
    &lt;input type="radio" id="r3q10" name="answer10" value="Answer 3"&gt;Answer C: &lt;?php echo $answerC10 ?&gt; &lt;br&gt;
  &lt;/div&gt;
    &lt;button class="checkButtons" onClick="calculateResults()" style="width:100px;"&gt; Submit Answers&lt;/button&gt;
&lt;/center&gt;
&lt;/div&gt;
</code></pre>

<p></p>