我正在开发具有PHP的在线检查系统。我设计了一种问题的问题类型,以便在考试中,每个学生都会从问题库收到随机问题。基本上,这意味着,如果我有100个问题,那么对于考试,我可以随机提取该论文60个问题。他们也随机拉动。
问题是用户何时刷新页面,他们每次都会收到新的随机问题。我试图将问题存储在HTML5 LocalStorage中,以及给定的答案。因此,希望从本地存储中加载而不是从每页刷新DB查询。
然后,我意识到PHP是服务的,当发生页面刷新时,我无法从LocalStorage加载到PHP脚本。一些文章据说使用Ajax。但是,当按下刷新按钮或F5时,我无法弄清楚如何将数据加载到同一页面。(是的,我尝试使用beforeunload)
Heres我作为解决方案所做的工作。我将加载的问题保存到了$ _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>
是的是,您遵循的步骤将起作用,并且关于已经标记的答案,您必须更新数据库并获得新的集合。
首页加载,然后保存所有问题ID和用户ID关系,例如
qid uid ans
1 1 update
1 5 update
2 5 update
因此,像这样的所有Queuestion是第一个以随机顺序使用用户ID的商店,并且在每个答案上更新此记录,我认为它很容易,因为如果用户关闭窗口或任何灯光关闭,请计算其易于检测到其左侧的位置
例如。如果Userd回答3问题,那么您可以检测到哪些尚未答案,然后仅提取这些问题或您要应用的任何其他逻辑,这对您来说很容易
因此,数据库是最好的解决方案,因为如果您使用更改浏览器或存在任何其他问题,则不会获得值。