这是检查被单击的收音机以与结果进行比较并向上滑动的脚本:
<script>
$(document).ready(function(){
$("input:radio").change(function(){
checkResult(this);
});
});
function checkResult(el)
{
//get the radio button value
var clickedvalue=$(el).val() ;
$this=$(el).parent("div.QA");
var hiddenanswer=$this.find(".hidden_result").val();
//call to next function to check result
var report=checkAnswer(clickedvalue,hiddenanswer);
if(report)
{
$this.find(".report").html("correct");
}
else{
$this.find(".report").html("Incorrect");
}
function checkAnswer(click,answer){
if(click==answer){
return true;
}
else{
return false;
}
}
$this.delay(500).slideUp();
}
</script>
这是从数据库中获取问题和选项的PHP 。我使用时间戳来name
为每个问题(收音机)设置不同的名称。
<?php
$dbconnect=mysqli_connect('localhost','root','','quiz')or die("Error Connecting to database"); $query="select * from question_answer ";
$result=mysqli_query($dbconnect,$query);
?>
<form method="get" action="">
<div id="container">
<?php
while($rows=mysqli_fetch_array($result))
{
echo '<div class="QA">';
echo '<h1>'.$rows['question'].'</h1>'.'<br/>';
echo '<input type="radio" class="check" name="'.$rows['radioname'].'" value="A">'.$rows['option1'].'</input>';
echo '<input type="radio" class="check" name="'.$rows['radioname'].'" value="A">'.$rows['option2'].'</input>';
echo '<input type="radio" class="check" name="'.$rows['radioname'].'" value="A">'.$rows['option3'].'</input>';
echo '<input type="radio" class="check" name="'.$rows['radioname'].'" value="A">'.$rows['option4'].'</input>';
echo '<br/>';
echo '<div class="report"></div>';
echo'</div>';
}
?>
</div>
</form>