如何在数组中存储单选按钮值并与正确答案进行比较?

时间:2014-02-06 10:19:22

标签: javascript php arrays

我正在使用名为“timer.To”的jQuery插件来定时考试。现在我想要的是当计时器达到0时,单选按钮值将存储在一个数组上然后将与正确的答案进行比较w / c也保存在一个数组上,计算正确答案的数量然后将其提交给数据库并转到下一页.. 这是计时器的代码..

<script type="text/javascript">
$(document).ready(function() {
  $('#countdown').timeTo(500, function() { // what should I do?  
      }); 
});
</script>

这是针对表单的,它是在外部文件上用PHP创建的:

$Element = 0;
$countme = 0;
$resulta = mysql_query("SELECT * FROM tblquestions WHERE subject = '".$subject."' ORDER BY rand()");
echo '<form action="" method="POST" > ';
    echo '<fieldset style="border: 2px solid grey; border-radius:7px; -moz-box-shadow:0 0 8px #666; -webkit-box-shadow:0 0 8px #666; box-shadow:0 0 8px #666;">';
    echo '<table width ="100%" height="100%" style="border: 2px solid grey; " >';

    while( $row = mysql_fetch_array($resulta))
    {
               $Data [$Element] = $row['CorrectAns']; // ARRAY WHERE THE CORRECT ANSWER IS SAVE

        echo "<tr style='border: 2px solid grey;'>";
        echo "<td style='border: 2px solid grey; font-weight:bold; width:130px;'>Question # " .$countme."</td>";
        echo "<td colspan='3' style='border: 2px solid grey; font-weight:bold;'>".$row['Questions']."</td>";
        echo "</tr>";

    $troll= 'QID'. $countme;  //USED FOR NAMING RADIO BUTTON..

        echo "<tr style='border: 2px solid grey;'>";
        echo "<td style='border: 2px solid grey;' ><label><input type='radio' name='".$troll."' value='".$row['choice4']."'>".$row['choice4']."</label></td>";
        echo "<td style='border: 2px solid grey;'><label><input type='radio' name='".$troll."' value='".$row['choice2']."'>".$row['choice2']."</label></td>";
        echo "<td style='border: 2px solid grey;'><label><input type='radio' name='".$troll."' value='".$row['choice3']."'>".$row['choice3']."</label></td>";
        echo "<td style='border: 2px solid grey;'><label><input type='radio' name='".$troll."' value='".$row['choice1']."'>".$row['choice1']."</label></td>";
        echo "</tr>";
    $rawr[$countme]= $row['choice4']; 
    $countme=$countme+1;
    $Element++;

        }
$correct = implode('', $Data); // VAR CONTAINING THE ARRAY OF CORRECT ANSWER
    echo "</table>";
    echo '</fieldset>';
        echo '<input type="submit" name="Submit" class="art-button" value="NEXT --> " />
    echo '</form>';

如果用户在时间用完之前完成,则提交按钮用于提交。请与您分享您的想法,谢谢:D

1 个答案:

答案 0 :(得分:0)

<script type="text/javascript">
$(document).ready(function() {
  $('#countdown').timeTo(500, function() { // call this function here.

validate_answer();
      }); 
});


function validate_answer()
{
// take your $data variable and check for correct answer 
//passing your array through loop. and find marks and display it.
}
</script>

也可以在提交时调用validate_answer()函数。