我正在进行一个测验页面。在这里有每个问题对应的单选按钮。我必须查看每个问题的状态是否回答。请给我任何解决方案来做到这一点。
这是我的代码
<?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>
答案 0 :(得分:0)
如果您试图阻止人们提交未经检查的问题答案 - 在您的情况下 - 最简单的方法是做每个答案:默认选中一个。
<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;
答案 1 :(得分:0)
<?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;
}
}
?>
答案 2 :(得分:0)
试试这个:
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;
};