我正在使用问题页面,每个用户都会向他显示2个或更多问题,每个问题有4个答案和4个(单选按钮),每个问题必须与其他问题分开,这基本上意味着任何单选按钮组都必须如此,我的问题就像下面的图片一样(我不能全部发布)
我已经使用了这个代码(没有工作)
$sql2 = "SELECT * FROM questions WHERE subject = 'web lang' LIMIT 5 ";
$result2 = mysqli_query($conn,$sql2);
echo "<form action='' method='POST'>";
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {
echo "<div align='center'>
<table>
<thead>
<tr >
<th >Q & A</th>
<th>Choose</th>
</tr>
</thead>
<tbody>
<tr>
<td>".$row2['question']."</td>
<td></td>
</tr>
<tr>
<td>".$row2['ans1']."</td>
<td><input type='radio' name='chk' value='1' >
</tr>
<tr>
<td>".$row2['ans2']."</td>
<td><input type='radio' name='chk' value='2' >
</tr>
<tr>
<td>".$row2['ans3']."</td>
<td><input type='radio' name='chk' value='3' >
</tr>
<tr>
<td>".$row2['ans4']."</td>
<td><input type='radio' name='chk' value='4' >
</tr>
</tbody>
</table><br> <br>
</div>
";
}
echo "<input type='submit' name='submit'>
</form>";
if (isset($_POST['submit'])) {
$chk = $_POST['chk'];
Echo $chk;
}
即使我像这样回音
print_r(array_values($chk));
如果每次我无法获得名称时都使用不同的名称,我会在while循环中使用这段代码,它可以工作,但如何获得其他表的值呢?
$new = 0;
<input type='radio' name='chk".$new."' value='some value' >
$new++;
试试这个:
$sql2 = "SELECT * FROM questions WHERE subject = 'web lang' LIMIT 5 ";
$result2 = mysqli_query($conn,$sql2);
echo "<form action='' method='POST'>";
$i=0;
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {
echo "<div align='center'>
<table>
<thead>
<tr >
<th >Q & A</th>
<th>Choose</th>
</tr>
</thead>
<tbody>
<tr>
<td>".$row2['question']."</td>
<td></td>
</tr>
<tr>
<td>".$row2['ans1']."</td>
<td><input type='radio' name='chk".$i."' value='1' >
</tr>
<tr>
<td>".$row2['ans2']."</td>
<td><input type='radio' name='chk".$i."' value='2' >
</tr>
<tr>
<td>".$row2['ans3']."</td>
<td><input type='radio' name='chk".$i."' value='3' >
</tr>
<tr>
<td>".$row2['ans4']."</td>
<td><input type='radio' name='chk".$i."' value='4' >
</tr>
</tbody>
</table><br> <br>
</div>
";
$i++;
}
echo "<input type='submit' name='submit'>
</form>";
if (isset($_POST['submit'])) {
$chk = $_POST['chk'];
Echo $chk;
}
假设你有一个与每个问题相关联的问题ID列,用以下方法改变你的,而
循环,
// your code
echo "<form action='' method='POST'>";
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {
?>
<div align='center'>
<table>
<thead>
<tr >
<th >Q & A</th>
<th>Choose</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row2['question']; ?></td>
<td></td>
</tr>
<tr>
<td><?php echo $row2['ans1']; ?></td>
<td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='1' >
</tr>
<tr>
<td><?php echo $row2['ans2']; ?></td>
<td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='2' >
</tr>
<tr>
<td><?php echo $row2['ans3']; ?></td>
<td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='3' >
</tr>
<tr>
<td><?php echo $row2['ans4']; ?></td>
<td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='4' >
</tr>
</tbody>
</table>
<br> <br>
</div>
<?php
}
echo "<input type='submit' name='submit'>
</form>";
// your code
稍后,当用户选择单选选项并点击提交按钮时,按以下方式获取问题ID和相应(给定)答案,
if (isset($_POST['submit'])) {
foreach($_POST['chk'] as $questionID => $answer){
// $questionID is the question ID and $answer is
// the corresponding given answer of that quesrtion
}
}