I'm building a really simple quiz: http://fh80.student.eda.kent.ac.uk/fyp-assets/quiz/quiz.php
At the end of the quiz it should display how many you got right e.g 5/10.
One thing about it is that each question is pulled in via AJAX so every question is a different .php file. Also, for every question there is the question page and an answer page. E.g question1.php
and question1a.php
I've created a variable on the page for the score:
/* Quiz Score */
var quizScore = 0;
When you load up question1.php
through AJAX, there is this script:
document.getElementById("nextQuestion1a").onclick = function(){
var correctAnswer = document.getElementById("lightningbolt");
if (correctAnswer.checked){
quizScore = quizScore+1;
}else{}};
The correct answer has an ID of lightningbolt
<input type="radio" name="question1" value="lightningbolt" id="lightningbolt">Lightning Bolt<br>
So, when this input/radio button is checked it should follow the function and +1 to quizScore;
if (correctAnswer.checked){
quizScore = quizScore+1;
I think this isn't working, because the event handler for doing this is onClick
of nextQuestion(ID)
- this is the same button which launches the AJAX which takes you to the next page (in this case, clicking nextQuestion1a
takes you to question1a.php
)
So I am thinking I need to bind it different somehow. Like not on click of that button, but just on check of the radio button.
At the end of the quiz I output the score as such:
<h3>
You got
<script>document.write(quizScore);</script>
/10
</h3>
But this doesn't output any value, not even 0 as the variable was initialised.
ETA: the variable does exist on the page at all times. The variable is not declared within the refreshed content. So the variable is always there.