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.
this 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">×</span> </a>
<div class = "pic">
<img src="Logo.png" width = "150" height = "150">
</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() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("score").innerHTML =
this.responseText;
}
};
xhttp.open("post", "examExtension.php", true);
xhttp.send();
}
</script>
and this for examExtension.php
<?php
$score = isset($_POST['score']) ? $_POST['score'] : '';
if ($score > 19 and $score < 21){
echo "A+";
}
if($score > 18 and $score< 20){
echo "A";
}
if($score > 17 and $score< 19){
echo "A-";
}
if($score > 16 and $score< 18){
echo "B+";
}
if($score > 15 and $score < 17){
echo "B";
}
if($score > 14 and $score< 16){
echo "B-";
}
if($score > 13 and $score< 15){
echo "C+";
}
if($score > 12 and $score < 14){
echo "C";
}
if($score > 11 and $score< 13){
echo "C-";
}
if($score > 10 and $score< 12){
echo "D+";
}
if($score > 9 and $score< 11){
echo "D";
}
?>
and lastly this is for the counting of scores it is based on how many radio buttons the student correctly clicked.
<script>document.getElementById("yes").addEventListener("click", function() {
let numberOfCorrectAnswers = document.querySelectorAll("input[type=radio].correct:checked").length;
document.getElementById("score").innerHTML = "Your Score: " + numberOfCorrectAnswers;
});
</script>
thanks in advance for the help