我正在开发在线测试应用程序,在一个页面中用options选择所有问题。我可以在提交时使用问题ID获取所有选定的单选按钮值。
<?php
include_once('connect.php');
$sql="select * from testquestions where testid='126'";
$result=mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result)>0)
{
$data = array(); // create a variable to hold the information
while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) !== false)
{
$data[] = $row; // add the row in to the results (data) array
}
//shift off the first value
array_shift($data[0]);
$merge = call_user_func_array('array_merge', $data);
$size=sizeof($merge);
$newdata=array();
$num=1;
echo "<form action='fetchquestion.php' method='post'>";
foreach ($merge as $key => $value )
{
$sql2="select qid, question,option1,option2,option3,option4 from question where qid='$value' ";
$result2=mysql_query($sql2) or die(mysql_error());
while($row2=mysql_fetch_assoc($result2))
{
$questionid=$row2['qid'];
$question=$row2['question'];
$option1=$row2['option1'];
$option2=$row2['option2'];
$option3=$row2['option3'];
$option4=$row2['option4'];
$newdata[]=$row2;
echo '<div><div class="question">';
echo "$num<h3>$question</h3>";
echo "</div>
<div class='answer'>
<ul>
<li><input type='radio' class='radioBtnClass' name='option' style='width:50px' value='1'/>$option1</li>
<li><input type='radio' class='radioBtnClass' name='option' style='width:50px' value='2'/>$option2</li>
<li><input type='radio' class='radioBtnClass' name='option' style='width:50px' value='3'/>$option3</li>
<li><input type='radio' class='radioBtnClass' name='option' style='width:50px' value='4'/>$option4</li>
<input type='hidden' name='count' value='$num' id='count'>
<input type='hidden' name='queId' value='$questionid' id='queId'>
</ul></div></div>";
$num++;
}
}
echo"<input type='submit' name='submit' value='Submit'><br>
</form>
";
if (isset($_POST['submit'])) {
$_SESSION['quiz'] = array();
// get rid of the submit element in the $_POST
unset($_POST['submit']);
// assign the values of the radio buttons to the $_SESSION['quiz'] array
$_SESSION['quiz'] = $_POST;
echo "submitted form";
}
else {
echo "Please submit the form.";
}
}
else
{
echo " no questions available for selected test";
}
?>
我怎样才能做到这一点? 有人可以帮忙吗?
答案 0 :(得分:0)
echo "</div>
<div class='answer'>
<ul>
<li><input type='radio' class='radioBtnClass' name='option_$questionid' style='width:50px' value='1'/>$option1</li>
<li><input type='radio' class='radioBtnClass' name='option_$questionid' style='width:50px' value='2'/>$option2</li>
<li><input type='radio' class='radioBtnClass' name='option_$questionid' style='width:50px' value='3'/>$option3</li>
<li><input type='radio' class='radioBtnClass' name='option_$questionid' style='width:50px' value='4'/>$option4</li>
<input type='hidden' name='count' value='$num' id='count'>
<input type='hidden' name='queId[]' value='$questionid' id='queId'>
</ul></div></div>";
表单提交后使用此代码
$data = $_POST;
$que_array = $data['queId'];
foreach($que_array as $key => $value){
$que_id = $value;
$option = 0;
if(isset($data['option_' . $que_id])){
$option = $data['option_' . $que_id];
}
echo $que_id . "--" . $option. "<br>";
}
答案 1 :(得分:0)
当您使用单选按钮的名称作为数组时。
因此,要获得所选无线电的价值,请使用此代码:
echo $_REQUEST['option'][0];