如何在用户选择他们的选择后在PHP中处理测验

时间:2016-07-02 14:59:01

标签: php

我正在设计一个测验应用程序,我正在使用while循环来加载数据库中的测验问题和选项,但我真的不知道如何处理测验的提交。我的意思是如何显示用户选择的选项。

我的问题是如何处理用户选择的选项?

以下是我显示测验问题和选项的代码:



<?php $response = mysql_query("SELECT * FROM iq_questions ORDER BY RAND()"); ?>
<form method='POST' id='quiz_form'>
	<?php 
		$counter = 0;
		while($result = mysql_fetch_array($response)){
		$counter = $counter + 1;
	?>
   <div id="question_<?php echo $result['question_id'];?>" class='questions'>
	<h4 id="question_<?php echo $result['question_id'];?>"><?php echo $counter.".  ".$result['question_name'];?></h4>
	<div class='align'>
	   <input type="radio" value="1" id='radio1_<?php echo $result['question_id'];?>' name='<?php echo $result['question_id'];?>'>
	   <label id='ans1_<?php echo $result['question_id'];?>' for='1'>A.  <?php echo $result['choice1'];?></label><br/>
	   <input type="radio" value="2" id='radio2_<?php echo $result['question_id'];?>' name='<?php echo $result['question_id'];?>'>
	   <label id='ans2_<?php echo $result['question_id'];?>' for='1'>B.  <?php echo $result['choice2'];?></label><br/>
	   <input type="radio" value="3" id='radio3_<?php echo $result['question_id'];?>' name='<?php echo $result['question_id'];?>'>
	   <label id='ans3_<?php echo $result['question_id'];?>' for='1'>C.  <?php echo $result['choice3'];?></label>
	   <input type="radio" checked='checked' value="5" style='display:none' id='radio4_<?php echo $result['question_id'];?>' name='<?php echo $result['question_id'];?>'>
	</div><br/>
   </div>
<?php } ?>
<input type="button" id='next<?php echo $result['question_id'];?>' value='Next!' name='question' class='btn btn-success'/>
</form> 
&#13;
&#13;
&#13;

我真的需要帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以将动作属性添加到表单中,该表单会在提交时将其发送到页面,您可以从表单中获取所有变量并根据需要显示它们。我相信这也是建议的做法即使您希望结果显示在与测验本身相同的页面上。

<form method='POST' id='quiz_form' action='result_page.php'>

然后使用'result_page.php'上的帖子显示它们

<?php echo $_POST["question_answer_1"]; ?>

HTML form action attribute