我正在设计一个在线多项选择测试,以便用户选择3个答案中的一个(单选按钮),然后提交他/她的答案。按下提交按钮后,我需要计算结果,我使用AJAX请求分别比较每个答案,并使用PHP脚本,该脚本也会根据AJAX请求参数逐个检索问题。
这是我的AJAX请求:
function calculateResults(){
totalPoints = 0;
var qNum = 10;
while(qNum != 0){
$.ajax({
method : "GET",
url: "php-scripts/testing-questions.php",
data: {queNum : qNum},
success: function(data){
console.log(data);
//check if radio1 value is set
if(document.getElementById('r1q' + qNum).check == true){
var answer = document.getElementById('r1q' + qNum).value;
}
//check if radio2 value is set
else if(document.getElementById('r2q' + qNum).check == true){
var answer = document.getElementById('r2q' + qNum).value;
}
else{ //radio 3 value is set then
var answer = document.getElementById('r3q' + qNum).value;
}
if(data === answer){
totalPoints++;
}
}
});
qNum--;
}
}
这是我的PHP脚本
if(isset($_GET['queNum'])){
$qNum = $_GET['queNum'];
getAnswer($qNum);
function getAnswer($num){
$query = mysqli_query($conn, "SELECT correctAnswer
FROM `Testing`
WHERE `questionNum` = '".$queNum."' ");
$row = mysqli_fetch_array($query,MYSQLI_ASSOC);
echo $row;
$correA = $row['correctAnswer'];
echo $correA;
}
}
请注意,$ conn变量是从config.php文件设置的,您可以认为与数据库的连接工作正常,函数中的echo语句也是出于调试原因,您还应该假设SQL查询运行良好。
我一直在努力寻找超过一天的错误,它看起来很简单,但我无法让它工作,没有其他的例子在这里或任何其他关于AJAX退出undefined.Anyone更多经验可以帮助我们多多赞赏。
如下所示的HTML代码:
<div class="container3">
<div id="startTest">
<center><button class="checkButtons" onClick="startTimer()" style="width:100px;"> Start Test</button></center>
</div>
<div id="beginTest">
<input type = "text" name="timer" width="30%" id="timer" value ="10:00" readonly>
</div>
<div id="questions">
<center>
<div class="question">
<p class="q">Question 1: <?php echo $question1 ?></p>
<input type="radio" id="r1q1" name="answer1" value="Answer 1" checked>Answer A: <?php echo $answerA1 ?> <br>
<input type="radio" id="r2q1" name="answer1" value="Answer 2">Answer B: <?php echo $answerB1 ?> <br>
<input type="radio" id="r3q1" name="answer1" value="Answer 3">Answer C: <?php echo $answerC1 ?> <br>
</div>
<div class="question">
<p class="q">Question 2: <?php echo $question2 ?></p>
<input type="radio" id="r1q2" name="answer2" value="Answer 1" checked>Answer A: <?php echo $answerA2 ?> <br>
<input type="radio" id="r2q2" name="answer2" value="Answer 2">Answer B: <?php echo $answerB2 ?> <br>
<input type="radio" id="r3q2" name="answer2" value="Answer 3">Answer C: <?php echo $answerC2 ?> <br>
</div>
<div class="question">
<p>Question 3: <?php echo $question3 ?></p>
<input type="radio" id="r1q3" name="answer3" value="Answer 1" checked>Answer A: <?php echo $answerA3 ?> <br>
<input type="radio" id="r2q3" name="answer3" value="Answer 2">Answer B: <?php echo $answerB3 ?> <br>
<input type="radio" id="r3q3" name="answer3" value="Answer 3">Answer C: <?php echo $answerC3 ?> <br>
</div>
<div class="question">
<p class="q">Question 4: <?php echo $question4 ?></p>
<input type="radio" id="r1q4" name="answer4" value="Answer 1" checked>Answer A: <?php echo $answerA4 ?> <br>
<input type="radio" id="r2q4" name="answer4" value="Answer 2">Answer B: <?php echo $answerB4 ?> <br>
<input type="radio" id="r3q4" name="answer4" value="Answer 3">Answer C: <?php echo $answerC4 ?> <br>
</div>
<div class="question">
<p class="q">Question 5: <?php echo $question5 ?></p>
<input type="radio" id="r1q5" name="answer5" value="Answer 1" checked>Answer A: <?php echo $answerA5 ?> <br>
<input type="radio" id="r2q5" name="answer5" value="Answer 2">Answer B: <?php echo $answerB5 ?> <br>
<input type="radio" id="r3q5" name="answer5" value="Answer 3">Answer C: <?php echo $answerC5 ?> <br>
</div>
<div class="question">
<p class="q">Question 6: <?php echo $question6 ?></p>
<input type="radio" id="r1q6" name="answer6" value="Answer 1" checked>Answer A: <?php echo $answerA6 ?> <br>
<input type="radio" id="r2q6" name="answer6" value="Answer 2">Answer B: <?php echo $answerB6 ?> <br>
<input type="radio" id="r3q6" name="answer6" value="Answer 3">Answer C: <?php echo $answerC6 ?> <br>
</div>
<div class="question">
<p class="q">Question 7: <?php echo $question7 ?></p>
<input type="radio" id="r1q7" name="answer7" value="Answer 1" checked>Answer A: <?php echo $answerA7 ?> <br>
<input type="radio" id="r2q7" name="answer7" value="Answer 2">Answer B: <?php echo $answerB7 ?> <br>
<input type="radio" id="r3q7" name="answer7" value="Answer 3">Answer C: <?php echo $answerC7 ?> <br>
</div>
<div class="question">
<p class="q">Question 8: <?php echo $question8 ?></p>
<input type="radio" id="r1q8" name="answer8" value="Answer 1" checked>Answer A: <?php echo $answerA8 ?> <br>
<input type="radio" id="r2q8" name="answer8" value="Answer 2">Answer B: <?php echo $answerB8 ?> <br>
<input type="radio" id="r3q8" name="answer8" value="Answer 3">Answer C: <?php echo $answerC8 ?> <br>
</div>
<div class="question">
<p class="q">Question 9: <?php echo $question9 ?></p>
<input type="radio" id="r1q9" name="answer9" value="Answer 1" checked>Answer A: <?php echo $answerA9 ?> <br>
<input type="radio" id="r2q9" name="answer9" value="Answer 2">Answer B: <?php echo $answerB9 ?> <br>
<input type="radio" id="r3q9" name="answer9" value="Answer 3">Answer C: <?php echo $answerC9 ?> <br>
</div>
<div class="question">
<p class="q">Question 10: <?php echo $question10 ?></p>
<input type="radio" id="r1q10" name="answer10" value="Answer 1" checked>Answer A: <?php echo $answerA10 ?> <br>
<input type="radio" id="r2q10" name="answer10" value="Answer 2">Answer B: <?php echo $answerB10 ?> <br>
<input type="radio" id="r3q10" name="answer10" value="Answer 3">Answer C: <?php echo $answerC10 ?> <br>
</div>
<button class="checkButtons" onClick="calculateResults()" style="width:100px;"> Submit Answers</button>
</center>
</div>
答案 0 :(得分:1)
首先看,您的代码没有显示任何问题。
你的PHP脚本应该是。 getAnswer($num)
应为getAnswer($qNum)
在查询中,应使用$qNum
变量代替$queNum
。
if(isset($_GET['queNum'])){
$qNum = $_GET['queNum'];
getAnswer($qNum);
//$qNum not $num
function getAnswer($qNum){
$query = mysqli_query($conn, "SELECT correctAnswer
FROM `Testing`
WHERE `questionNum` = '".$qNum."' ");
$row = mysqli_fetch_array($query,MYSQLI_ASSOC);
$correA = $row['correctAnswer'];
echo $correA;
exit;
}
}