我正在进行在线考试..数据从页面传递到考试页面。但是单选按钮的值没有从检查页面传递到结果页面。我已经检查了课程名称和科目名称是否正在通过。我添加了计时器部分。哪个好用。 while内的单选按钮的值不会传递给结果..
考试代码
<html>
<head>
<title>EXAM</title>
</head>
<body>
<?PHP
//$i=1;
$j=1;
$user_name = "root";
$password = "";
$database = "online_exam";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database);
if ($db_found)
{ session_start();
$i=$_SESSION['$i'];
$sub = $_SESSION['$sub'];
/*if(isset($_POST['b_subject']))
{
$selected_button=$_POST['subject'];
if($selected_button=='subject1')
$_SESSION['$sub']= "GK";
else if($selected_button=='subject2')
$_SESSION['$sub']="math";
else
print "error";
}*/
if(isset($_POST['subject'])){
$selected_button=$_POST['subject'];
}
else
{
$selected_button="default button";
}
while($j<$i)
{
if($selected_button=='subject'.$j)
$s= $sub[$j];
$j++;
}
//print $_SESSION['$sub'];
//print $selected_button;
//print $_SESSION['$cour'];
//print
// //assign $_SESSION['array'] to $array
//print $sub[1];
/* foreach($sub as $value) {
print $value; //print $array contents
}*/
$c = $_SESSION['$cour'];
//$s = $_SESSION['$sub'];
$SQL = "SELECT ques FROM ques_ans WHERE course='$c' AND subj='$s'";
$result = mysql_query($SQL);
echo '<FORM name ="form1" method ="post" action="result_a.php" >';
while ( $db_field = mysql_fetch_assoc($result) )
{
print $db_field['ques']."<BR>";
?>
<Input type = 'Radio' Name ='ques<?PHP echo $i;?>' value= '1'>YES
<Input type = 'Radio' Name ='ques<?PHP echo $i;?>' value= '0'>NO
<br>
<?PHP
$i++;
}
$_SESSION['$i']= $i;
//$_SESSION['ques'] = $que;
$_SESSION['$sub'] = $s;
echo '<input type="submit" name="submit" id="submit" value="SUBMIT"/></FORM>';
mysql_close($db_handle);
}
$targetDate = time()+(330*60) + (1*30);
$actualDate = time()+(330*60);
$secondsDiff = $targetDate - $actualDate;
$remainingDay = floor($secondsDiff/60/60/24);
$remainingHour = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
$remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60);
$remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60));
?>
<h1>Time Left</h1>
<script type="text/javascript">
var hours = <?php echo $remainingHour; ?>
var minutes = <?php echo $remainingMinutes; ?>
var seconds = <?php echo $remainingSeconds; ?>
function setCountDown ()
{
seconds--;
if (seconds < 0){
minutes--;
seconds = 59
}
if (minutes < 0){
hours--;
minutes = 59
}
if (hours < 0){
days--;
hours = 23
}
document.getElementById("remain").innerHTML = hours+" hours : "+minutes+" minutes : "+seconds+" seconds";
SD=window.setTimeout( "setCountDown()", 1000 );
if (minutes == '00' && seconds == '00') { seconds = "00"; window.clearTimeout(SD);
window.alert("Time is up. Press OK to continue.");
window.location = "http://localhost/result_a.php"
}
}
</script>
<body onload="setCountDown();">
<div id="remain"><?php echo "$remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?></div>
</body>
</html>
结果内线&#34; $ selected_radio = $ _POST [&#39; ques&#39;。$ i];&#34;显示错误&#34;未定义的索引:ques1&#34; ,&#34;未定义的索引:ques2&#34;,......等......
结果代码
<html>
<head>
<title>RESULT</title>
<body>
<?PHP
$i=1;
$count=0;
$ans= "";
session_start();
//$i=$_SESSION['$i'];
$s=$_SESSION['$sub'];
$c=$_SESSION['$cour'];
//$que[]=$_SESSION['ques'];
//print $c.$s;
$user_name = "root";
$password = "";
$database = "online_exam";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database,$db_handle);
$SQL = "SELECT ans FROM ques_ans WHERE course='$c' AND subj='$s'";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_array($result) )
{
$selected_radio = $_POST['ques'.$i];
if ($selected_radio == '1')
{
$ans='1';
}
else if ($selected_radio == '0')
{
$ans='0';
}
if($ans==$db_field['ans'])
$count++;
$i++;
}
print $count;
?>
</body>
</html>
</head>
答案 0 :(得分:0)
在尝试执行$i =
之前,你永远不会定义$i++;
,你不能在不声明它的起始值的情况下自动增加整数。
因此,您的字段实际上只是
name="ques"
没有附加任何整数。定义$i = 1;
或$i = 0
;根据您的愿望,这将得到解决。