简体   繁体   English

如何使用 PHP 和/或 JavaScript 将用户的分数存储在我网站上的变量中?

[英]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">&times;</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>

模态 2 截图

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).您可以使用localStoragesessionStorage ,它可以将信息保存到 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.

相关问题 如何在我的网站上存储永久变量? - How can I store a permanent variable on my website? 如何将 JavaScript 变量输出存储到 PHP 变量中? - How can I store JavaScript variable output into a PHP variable? 如何在用户的浏览器中缓存我的网站? - How can I cache my website in the user's browser? 如何使用Javascript检测用户对网站的首次访问? - How I can detect the first access of user in my website using Javascript? 如果用户使用javascript从其他选项卡中的Facebook注销,如何在我的网站中收到通知? - How can I get notified in my website if user log out from facebook in other tab using javascript? 使用 HTML 和 JavaScript,我如何存储用户的选择并将这些值作为输入参数传递? - Using HTML and JavaScript, how can I store a user's selection and pass those values as an input parameter? 如何通过 PHP、Javascript 或 API 使用我的网站连接到 vimeo? - How can I connect to vimeo by using my website with PHP, Javascript or an API? 如何使用 PHP 和 JavaScript 将歌曲从我的网站分发到流媒体网站 - How can I distribute songs from my website to streaming websites using PHP and JavaScript 如何将我的 php 变量的值复制到用户的剪贴板 - How can i copy value of my php variable to user's clipboard 如何在用户页面上保护我的 PHP cookies 免受 JavaScript 的影响? - How can I protect my PHP cookies from JavaScript on user's pages?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM