How to determine the score and letter grade using JS PHP AJAX

So I made a program about an exam and I need to determine the letter grades of students but first, they have to answers some questions then base on what score the student gets the student will get a letter grade, someone recommended me to use AJAX because I need to do the process itself in the form without refreshing so I tried to make it but I ended up getting confused can someone help me, please.

I’m confused because everytime I click the button the result is always no score has been given

Here are the codes:

 <div class = "form">
<button id = "submit" name = "submit"  value = "submit" > SUBMIT </button>
<div id="myModal" class="modal">
  <div class="modal-content">
    <h3> Are you sure you want to submit? </h3>
    <button id = "yes" name = "yes" class = "yes" value = "submit" onclick ="loadDoc()"> YES </button>
    <button id = "no" name = "no" class = "no"> NO </button>
  </div>
</div>


<div id="myModalLast" class="modalLast">
  <div class="modal-contentLast">
   <a href = "personal.php"> <span class="close">&times;</span> </a>

<div class = "pic">
   
</div>  
    <h3> Full name: Cathleen Joyce Imperial Almeda </h3>  
    <h3> Total items:20 <p id = "score" name = "scorename"></p> </h3>  
   <h1> <br><p id = "scores" name = "realscores"></p>

   Rank:<p id = "rank"></p>
</h1> 

  </div>
</div>
</div>

this is for the ajax part:

  <script>

function loadDoc(score) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      scoreElement.innerHTML = "Your Score: " + this.responseText;
      console.log('Grade calculation complete');
    }
  };
  xhttp.open("post", "examExtension.php", true);


  xhttp.send('score=' + score);
}
</script>

And this for examExtension.php:

   <?php


if (!isset($_POST['score'])) {
  echo 'No score has been given';
  exit;
}



$score = intval($_POST['score']);

if ($score > 19 and $score < 21) {
  echo "A+";
} else if ($score > 18 and $score < 20) {
  echo "A";
} else if($score > 17 and $score < 19) {
  echo "A-";
} else if ($score > 16 and $score < 18) {
  echo "B+";
} else if ($score > 15 and $score < 17) {
  echo "B";
} else if ($score > 14 and $score < 16) {
  echo "B-";
} else if ($score > 13 and $score < 15) {
  echo "C+";
} else if ($score > 12 and $score < 14) {
  echo "C";
} else if ($score > 11 and $score < 13) {
  echo "C-";
} else if ($score > 10 and $score < 12) {
  echo "D+";
} else if($score  > 9 and $score < 11) {
  echo "D";
}

exit;

?>

And lastly, this is for the counting of scores it is based on how many radio buttons the student correctly clicked.

    <script>
const scoreElement = document.getElementById("score");
const yesButton = document.getElementById("yes");

yesButton.addEventListener("click", function() {
  let numberOfCorrectAnswers = document.querySelectorAll("input[type=radio].correct:checked").length;


  loadDoc(numberOfCorrectAnswers);
  console.log('Calculating grade');
});
</script>

3 posts were merged into an existing topic: Js, PHP WITH AJAX: how to determine the letter grade of students using javascript with ajax?

Sponsor our Newsletter | Privacy Policy | Terms of Service