![](/img/trans.png)
[英]How to keep score for questions answered correct and deduct points for questions answered wrong?
[英]how to highlight status of each questions whether answered or not
我正在做一个测验页面。这里有与每个问题相对应的单选按钮。我必须查看每个问题的状态,无论是否回答。请给我任何解决方案来做到这一点。
这是我的代码
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$rs=mysql_query("select * from question where testid=$tid order by quesid ",$cn) or die(mysql_error());
if($_SESSION[qn]>mysql_num_rows($rs)-1)
{
unset($_SESSION[qn]);
echo "<h1 class=\"head1\">Some Error Occured</h1>";
session_destroy();
echo "Please <a href=\"UserHome.php\"> Start Again</a>";
exit;
}
?>
<form name="myfm" id="myfm" method="post" action="QuizSub.php">
<table width="100%">
<?php
$n=0;
while($row= mysql_fetch_row($rs)){?>
<tr> <td width="30"></td><td></td></tr>
<?php $n=$n+1; ?>
<tr><td>Question <?php echo $n.") ".$row[2]; ?></td></tr>
<tr><td class="style8">A. <input type="radio" name="ques<?php echo $n; ?>[]" value="1"><?php echo $row[3]; ?></td></tr>
<tr><td class="style8">B. <input type="radio" name="ques<?php echo $n; ?>[]" value="2"><?php echo $row[4];?></td></tr>
<tr><td class="style8">C. <input type="radio" name="ques<?php echo $n; ?>[]" value="3"><?php echo $row[5];?></td></tr>
<tr><td class="style8">D. <input type="radio" name="ques<?php echo $n; ?>[]" value="4"><?php echo $row[6];?></td></tr>
<?php
}
echo "<tr><td><input type=\"hidden\" name=\"qncount\" id=\"qncount\" value=\"".$n."\"><input type=\"submit\" name=\"submit\" id=\"result\" value=\"Get Result\"></td></tr>";
?>
</table>
</form>
如果您试图阻止人们提交未经检查的问题答案 - 在您的情况下 - 最简单的方法是对每个答案:默认选中一个。
<input type="radio" checked />
对于无人参与的单选按钮,定位您正在使用的名称,即:
<form method="post">
<input type="radio" value="1" name="example">
</form>
然后将其 POST 到一个 PHP 文件。 然后,您可以通过执行以下操作来检查它是否无人看管:
<?php
if(!isset($_POST['example'])): // ! represents NOT
// do something
endif;
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$rs=mysql_query("select * from question where testid=$tid order by quesid ",$cn) or die(mysql_error());
if($_SESSION[qn]>mysql_num_rows($rs)-1)
{
unset($_SESSION[qn]);
echo "<h1 class=\"head1\">Some Error Occured</h1>";
session_destroy();
echo "Please <a href=\"UserHome.php\"> Start Again</a>";
exit;
}
?>
<form name="myfm" id="myfm" method="post" action="QuizSub.php">
<table width="100%">
<?php
$n=0;
while($row= mysql_fetch_row($rs)){?>
<tr> <td width="30"></td><td></td></tr>
<?php $n=$n+1; ?>
<tr><td>Question <?php echo $n.") ".$row[2]; ?></td></tr>
<tr><td class="style8">A. <input type="radio" name="ques<?php echo $n; ?>[]" value="1"><?php echo $row[3]; ?></td></tr>
<tr><td class="style8">B. <input type="radio" name="ques<?php echo $n; ?>[]" value="2"><?php echo $row[4];?></td></tr>
<tr><td class="style8">C. <input type="radio" name="ques<?php echo $n; ?>[]" value="3"><?php echo $row[5];?></td></tr>
<tr><td class="style8">D. <input type="radio" name="ques<?php echo $n; ?>[]" value="4"><?php echo $row[6];?></td></tr>
<?php
}
echo "<tr><td><input type=\"hidden\" name=\"qncount\" id=\"qncount\" value=\"".$n."\"><input type=\"submit\" name=\"submit\" id=\"result\" value=\"Get Result\"></td></tr>";
?>
</table>
</form>
将 QuizSub.php 保存如下
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['submit'] == "Get Result")
{
if(ctype_digit($_POST['qncount']) && $_POST['qncount'] > 0)
{
for($i=1;$i<=$_POST['qncount'];$i++)
{
if(isset($_POST['ques'.$i]))
echo "Q".$i." is answered and the answer is".$_POST['ques'.$i][0] ;
else
echo "Q".$i." is not answered" ;
echo "<br/>";
}
}
else
{
echo "Questions are empty";
exit;
}
}
?>
尝试这个 :
function checkAnsweredOrNot() {
var radioButtons = document.myfm.radioGroupName;
var answered;
for(var i=0; i<radioButtons.length; i++) {
if( radioButtons[i].checked ) {
answered = true;
break;
}
}
answered = false;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.