<?php
include "connect.php";
session_start();
$servedQuestions = array(0);
$c="SELECT * from `qa`;";
$r=mysqli_query($conn,$c);
$count=mysqli_num_rows($r);
while(count($servedQuestions) <= 5) {
$n1 = mt_rand(1,$count);
if (in_array($n1, $servedQuestions))
continue;
$servedQuestions[] = $n1;
}
$selected=[];
for($i=1;$i<count($servedQuestions);$i++){
echo "<br>";
$get_question="SELECT * from `qa` WHERE qno =
`'".$servedQuestions[$i]."';";
$result=mysqli_query($conn,$get_question);
$row=mysqli_fetch_array($result);
$qno=$row['qno'];
$question=$row['question'];
$opt1=$row['opt1'];
$opt2=$row['opt2'];
$opt3=$row['opt3'];
$opt4=$row['opt4'];
echo "QUESTION: $i <br> <br>";
echo"$question <br><br> ";
echo"<input type='radio'name='response[$qno]'value='1'>$opt1<br> ";
echo"<input type='radio' name='response[$qno]' value='2'>$opt2<br> ";
echo"<input type='radio' name='response[$qno]' value='3'>$opt3 <br>";
echo"<input type='radio' name='response[$qno]' value='4'>$opt4<br>";
echo "<br>";
$v=$_POST['response['.$qno.']'];
echo $v;
?>
初始代码为这些数字生成5个随机数,显示5个随机问题。当我试图回显单选按钮的值时,它说未定义索引.im无法打印单选按钮的值。
答案 0 :(得分:0)
而不是命名您的单选按钮,如:
<input type='radio'name='response[$qno]'value='1'>$opt1<br>
您可以这样命名:
<input type='radio'name='$qno_response'value='1'>$opt1<br>
在后端(php
),您可以访问单选按钮:
for($i=1;$i<count($servedQuestions);$i++)//Or you can have your own way of getting the list of questions generated
{
$QuestionAnswer = $_POST[$qno.'_response'];
//This will work like:
//$QuestionAnswer = $_POST['1_response']; //for first question
//$QuestionAnswer = $_POST['2_response']; //for second question and so on..
}