[英]How can I store a user's score in a variable on my website using PHP and/or JavaScript?
Right now I am displaying a question and its possible answers in a modal on my page (shown below).现在,我正在我的页面上以模式显示一个问题及其可能的答案(如下所示)。 The "submit answer" button will post the form but I can't use it in my situation (I explain this at the bottom of this post) so please ignore it. “提交答案”按钮将发布表单,但我无法在我的情况下使用它(我在这篇文章的底部解释了这一点)所以请忽略它。
I use the HTML & PHP code below to do retrieve the question and answers from my database.我使用下面的 HTML 和 PHP 代码从我的数据库中检索问题和答案。
<!-- The Modals -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<!-- display question as heading -->
<?php
while ( $row = $result->fetch_assoc() ) {
echo "<h3>" . $row[ "question" ] . "</h3>";
}
?>
<button id="close1" class="close">×</button>
</div>
<div class="modal-body">
<form action="#" method="post" id="quiz">
<!-- display answers as radio -->
<div>
<?php
while ($id = $choice_idResult1->fetch_assoc()) {
echo '<input type= "radio" name="question1_answers" id="question1_answers_A" value= "' . $id["choice_id"] . '"';
}
?>
<label for="question1_answers_A">
<?php while($row = $result2->fetch_assoc()) {echo $row["choice_text"];}
$answer1 = $row;
?>
</label>
</div> .....
When the "Open Modal 2" button is clicked, the JavaScript code below closes the modal and opens the next.单击“打开模态 2”按钮时,下面的 JavaScript 代码将关闭模态并打开下一个。
btn2.onclick = function() {
checkRadio('question1_answers_B');
modal.style.display = "none";
modal2.style.display = "block";
}
As you can see, I've called a function called checkRadio
and passes it the id of the radio input that is the correct answer.如您所见,我调用了名为checkRadio
并将其传递给正确答案的无线电输入的 id。
checkRadio
then uses an if statement to check whether that radio is checked and if it is it calls another function that increments a variable tally
.然后checkRadio
使用 if 语句来检查是否检查了该无线电,如果是,它会调用另一个 function 来增加变量tally
。
var tally = 0;
function checkRadio(id) {
if (document.getElementById(id).checked) {
addTally();
}
}
function addTally() {
tally++;
}
I then have a function called showTally
that I want to display the tally in the header of the next modal (for testing purposes).然后我有一个名为showTally
的 function 我想在下一个模态的 header 中显示计数(用于测试目的)。 However, nothing shows up in the next modal no matter what I select.但是,无论我是什么 select,下一个模态中都没有显示任何内容。
function showTally() {
document.getElementById("tally").innerHTML = tally;
}
And in the modal header:在模态 header 中:
<h3 id="tally"></h3>
In plain English, I need to be able to identify which radio input has been selected by the user so that I can check it is the correct answer and add to the tally which will be displayed after the last question.I can't actually post the form as it will close the modal.用简单的英语,我需要能够识别用户选择了哪个无线电输入,以便我可以检查它是正确的答案并添加到将在最后一个问题之后显示的计数中。我实际上无法发布表单,因为它将关闭模式。
If you need more screenshots or information please let me know.如果您需要更多屏幕截图或信息,请告诉我。
I may be misunderstanding the question, but this may help you.我可能误解了这个问题,但这可能会对您有所帮助。
var myAnswers1 = document.querySelector("question1_answers");
for(var i=0;i<myAnswers1.length;i++)
{
console.log("Question 1, radio index " + i + " is " + myAnswers1[i].checked + ".");
}
Sample Output:样品 Output:
Question 1, radio index 0 is false.
Question 1, radio index 1 is true.
Question 1, radio index 2 is true.
You can use localStorage
or sessionStorage
, it can save info to the session, the info exists when you refresh or redirect html page (same domain).您可以使用localStorage
或sessionStorage
,它可以将信息保存到 session,当您刷新或重定向 html 页面(同域)时,该信息存在。 Here is usage below:下面是用法:
var tally = localStorage.getItem('tally') || 0
function addTally() {
tally++;
localStorage.setItem('tally', tally)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.