gpt4 book ai didi

javascript - 在其他 php 文件中使用 Javascript 显示表单结果

转载 作者:行者123 更新时间:2023-12-03 10:20:24 25 4
gpt4 key购买 nike

这是我的 rat.php 文件,它显示了包含 8 个问题的问题形式。我使用 onsubmit="getScore(this.form) 来引用我的 Javascript 代码。我的 Javascript 代码位于显示文件中,即 ratResult.php

但是,我的显示文件仅捕获所选的答案,而不捕获百分比。没有显示错误,但百分比是我最需要的东西。

<form method="POST" action="ratResult.php" onsubmit="getScore(this.form)">

<div id="form" style="text-align:left">
<br><h2>Please answer all questions</h2><br>
<?PHP print question(1); ?>
<br><input type = "radio" name = "rat1" value = "<?PHP print ansHigh(1); ?>"><?PHP print ansHigh(1); ?><br>
<input type = "radio" name = "rat1" value = "<?PHP print ansLow(1); ?>"><?PHP print ansLow(1); ?><br><br>
<?PHP print question(2); ?>
<br><input type = "radio" name = "rat2" value = "<?PHP print ansHigh(2); ?>"><?PHP print ansHigh(2); ?><br>
<input type = "radio" name = "rat2" value = "<?PHP print ansLow(2); ?>"><?PHP print ansLow(2); ?><br><br>

这是文件 ratResult.php 中的部分,显示所有选择的答案和百分比。

<div id="test" style="text-align:left">
<?php


if(empty($_POST)===false){

$rat1 = $_POST['rat1'];
$rat2 = $_POST['rat2'];
$rat3 = $_POST['rat3'];
$rat4 = $_POST['rat4'];
$rat5 = $_POST['rat5'];
$rat6 = $_POST['rat6'];
$rat7 = $_POST['rat7'];
$rat8 = $_POST['rat8'];

echo "<h4>RAT</h4>";
echo "Answer 1: ".$rat1."<br>";
echo "Answer 2: ".$rat2."<br>";
echo "Answer 3: ".$rat3."<br>";
echo "Answer 4: ".$rat4."<br>";
echo "Answer 5: ".$rat5."<br>";
echo "Answer 6: ".$rat6."<br>";
echo "Answer 7: ".$rat7."<br>";
echo "Answer 8: ".$rat8."<br>";

$insert = mysql_query('INSERT INTO ratresult(username,q1,q2,q3,q4,q5,q6,q7,q8,dateTaken)VALUES("'.$loginSession.'","'.$rat1.'"
,"'.$rat2.'","'.$rat3.'","'.$rat4.'","'.$rat5.'","'.$rat6.'","'.$rat7.'","'.$rat8.'",now())') or die (mysql_error($insert));
}
else
echo "error!";

?>
Score = <input type=text size=15 name="percentage"><br>
<hr /><a href=main.php>Back To Main Page </a>

这是计算百分比的 Java 脚本。

<script>
var numQues = 8;
var numChoi = 2;
var answers = new Array(8);
answers[0] = "Yes";
answers[1] = "Above 50 years old";
answers[2] = "Below 12 years old";
answers[3] = "Yes";
answers[4] = "No";
answers[5] = "Yes";
answers[6] = "Yes";
answers[7] = "Above 50 years old";
function getScore(form) {
var score = 0;
var currElt;
var currSelection;
for (i=0; i<numQues; i++) {
currElt = i*numChoi;
for (j=0; j<numChoi; j++) {
currSelection = form.elements[currElt + j];
if (currSelection.checked) {
if (currSelection.value == answers[i]) {
score++;
break;
}
}
}
}

score = Math.round(score/numQues*100);
form.percentage.value = score + "%";
var correctAnswers = "";
for (i=1; i<=numQues; i++) {
correctAnswers += i + ". " + answers[i-1] + "\r\n";
}
form.solutions.value = correctAnswers;
}
</script>

最佳答案

我在 JavaScript 中没有看到任何明确说明百分比必须去往何处的内容。您有一个名为“percentage”的表单字段,但它没有 ID,JavaScript 将获取 ID 而不是名称。所以我将其重新定义为:

<input type=text size=15 name="percentage" id="percentage">

然后在 JavaScript 中你可以清楚地说明结果的去向:

document.getElementById("percentage").value = score + "%";

关于javascript - 在其他 php 文件中使用 Javascript 显示表单结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29661463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com