我正在开发一个使用php的在线考试系统。我设计了一个问题库类型的东西,以便在考试中每个学生从问题库中随机提问。基本上这意味着,如果我有100个问题,那么考试就可以随机抽出60个问题。他们也随机抽取。在线考试系统中不时给出答案
问题是当用户刷新页面时,他们每次都会得到新的随机问题。我尝试将HTML5 localStorage中的问题与给定的答案一起存储。所以希望从本地存储加载,而不是在每个页面刷新时从数据库查询。
然后我意识到php是serverside,当发生页面刷新时,我无法从localStorage加载到php脚本。有些文章说使用ajax。但我不知道如何加载数据,当刷新按钮或F5按下时,进入相同的页面。 (是的,我试着用onbeforeunload)
继承人我做了什么解决方案。我已经将加载的问题保存到$ _SESSION并访问该问题以保持刷新时的相同问题。
问题,我目前有虽然这个工程保持它不利于保存已标记的答案,如果用户不小心关闭了窗口,他们不能继续在那里他们都不放过的问题。
任何想法?如果你想看看我的代码,请让我知道。 谢谢。
更新这里是我的代码
<?php if($_GET['exam_id']!="")
{
$e_id=decrypt_string($_GET['exam_id']);
$query_p_exam=mysqli_fetch_array(mysqli_query($conn,"select * from exam where e_id='".$e_id."'"));
$category_id=$query_p_exam['category_id'];
$subcategory_id=$query_p_exam['subcategory_id'];
$subject_id=$query_p_exam['subject_id'];
$exam_name=$query_p_exam['exam_name'];
$exam_duration=$query_p_exam['exam_duration'];
$neg_mark_status=$query_p_exam['neg_mark_status'];
$negative_marks=$query_p_exam['negative_marks'];
$question_limit = $query_p_exam['question_limit'];
//echo $question_limit;
}
?>
<div class="main-content">
<br>
<div class="container-fluid padded">
<div class="container-fluid padded">
<div class="row-fluid">
<div class="span8">
<div class="box">
<div class="box-header">
<span class="title"><i class="icon-reorder"></i> Questions</span>
</div>
<div class="box-content scrollable" style="max-height: 500px; overflow-y: auto">
<div class="box-section news with-icons">
<form method='post' id='quiz_form' action="main_result.php">
<table style="width:100%;vertical-align:top" class="table table-normal">
<tr>
<td style="vertical-align:top">
<?php
if($_SESSION["rows"]){
$tempArray = $_SESSION["rows"];
//var_dump($tempArray);
$total_querstion= $question_limit;
$timeduration=60000*$query_exam_Name['duration'];
$i=1;
$m_e_id=1;
foreach($tempArray as $result){
$rows[] = $result;
?>
<!-- DISPLAY QUESTION BEGINS -->
<div <?php if($i==1){}else{echo 'style="display: none;"';}?> id="<?php echo $m_e_id;?>_<?php echo $i;?>" class="display_question">
<div class="bradcome-menu qu-pa">
<div class="col-md-6"> <span class="question"> Question No. <?php echo $i;?></span></div>
</div>
<h4 class="quction"><p><?php echo $result['question'];?></p></h4>
<?php if($result['typeofquestion']=='Single'){?>
<table class="answeers" border="0" width="100%">
<tbody>
<tr>
<td style="width:10px"><input name="radio_<?php echo $result['q_id'];?>" value="A" id="" type="radio"></td>
<td><?php echo $result['option_a'];?> </td>
</tr>
<tr>
<td>
<input name="radio_<?php echo $result['q_id'];?>" value="B" id="" type="radio"></td><td>
<?php echo $result['option_b'];?> </td>
</tr>
<tr>
<td>
<input name="radio_<?php echo $result['q_id'];?>" value="C" id="" type="radio"></td><td>
<?php echo $result['option_c'];?> </td>
</tr>
<tr>
<td>
<input name="radio_<?php echo $result['q_id'];?>" value="D" id="" type="radio"></td><td>
<?php echo $result['option_d'];?> </td>
</tr>
</tbody>
</table>
<?php }
if($result['typeofquestion']=='Multiple'){?>
<table class="answeers" border="0" width="100%">
<tbody><tr>
<td style="width:10px">
<input name="checkbox_A_<?php echo $result['q_id'];?>" value="A" id="" type="checkbox"></td><td>
<?php echo $result['option_a'];?> </td>
</tr>
<tr>
<td>
<input name="checkbox_B_<?php echo $result['q_id'];?>" value="B" id="" type="checkbox"></td><td>
<?php echo $result['option_b'];?> </td>
</tr>
<tr>
<td>
<input name="checkbox_C_<?php echo $result['q_id'];?>" value="C" id="" type="checkbox"></td><td>
<?php echo $result['option_c'];?> </td>
</tr>
<tr>
<td>
<input name="checkbox_D_<?php echo $result['q_id'];?>" value="D" id="" type="checkbox"></td><td>
<?php echo $result['option_d'];?> </td>
</tr>
</tbody>
</table>
<?php }?>
</div>
<!-- DISPLAY QUESTION ENDS -->
<?php $i++;}
}else{
$query_pag_data = mysqli_query($conn,"SELECT * from question where e_id='".$e_id."' and question_status=1 and c_id='".$category_id."' and s_c_id='".$subcategory_id."' order by rand() LIMIT ".$question_limit);
$total_querstion= $question_limit;
$timeduration=60000*$query_exam_Name['duration'];
$i=1;
$m_e_id=1;
while($result=mysqli_fetch_array($query_pag_data)){
$rows[] = $result;
?>
<!-- DISPLAY QUESTION BEGINS -->
<!-- same code -->
<!-- DISPLAY QUESTION ENDS -->
<?php $i++;
$_SESSION["rows"] = $rows;
}
}
?>
<?php
$json = json_encode($rows);
$_SESSION ['json'] = $json;
?>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td><div id="prev" class="btn btn-gray "><?php echo constant('TI_PREVIOUS_BUTTON');?></div></td>
<td><div id="mnext" class="btn btn-gray"><?php echo constant('TI_MARK_FOR_REVIEW_BUTTON');?></div></td>
<td><div id="next" class="btn btn-gray"><?php echo constant('TI_NEXT_BUTTON');?></div></td>
<td><div id="clearAnswer" class="btn btn-gray"><?php echo constant('TI_CLEAR_ANSWER_BUTTON');?></div></td>
<td><div style="float:right"><input id="finish" class="btn btn-green" value="Finish" name="Finish" onclick="return confirm('<?php echo constant('TI_SUBMITALERT_MESSAGE')?>')" type="submit"></div></td>
</tr>
</table>
</td>
</tr>
</table>
<input type="hidden" name="exam_id" value="<?php echo $_GET['exam_id'];?>">
</form>
</div>
</div>
</div>
</div>
请问您可以发布代码 –
欢迎来到Stack Overflow!你可以先参加[游览]并学习[问]一个很好的问题,然后创建一个[mcve]。这使我们更容易帮助你。 – Katie
我已更新我的问题并添加了代码。请看一下。 – user3231665