问题是我无法将HTML数组的输入发布到php数组变量。
X单选按钮组说答案[1],答案[2] ....答案[x]。答案[1]是有四个值A,B,C,D选择单选按钮值存储在答案[1]中,所有答案[2]相同,....答案[x]。所以基本上我想将每个选定的选项存储在数组中,即数组位置 1 处 1 的正确答案等等......我正在尝试获得客观问题的答案,并在提交时将其保存到数组变量中,然后在单列中插入数据库。
当用户输入问题编号(例如10个)时,他将获得每个问题的A,B,C,D单选按钮,即10次A,B,C,D,可以选择正确答案并提交。将所有答案保存在数组中并插入表中。
HTML 输出显示为...
<tr>
<td>1      <input type="radio" name="answer[1]" value="A">   A   <input type="radio" name="answer[1]" value="B">   B  <input type="radio" name="answer[1]" value="C">   C   <input type="radio" name="answer[1]" value="D">  D
</td>
</tr>
<tr>
<td>2      <input type="radio" name="answer[2]" value="A">   A   <input type="radio" name="answer[2]" value="B">   B  <input type="radio" name="answer[2]" value="C">   C   <input type="radio" name="answer[2]" value="D">  D
</td>
</tr>
<tr>
<td>3      <input type="radio" name="answer[3]" value="A">   A   <input type="radio" name="answer[3]" value="B">   B  <input type="radio" name="answer[3]" value="C">   C   <input type="radio" name="answer[3]" value="D">  D
</td>
</tr>
<tr>
<td>4      <input type="radio" name="answer[4]" value="A">   A   <input type="radio" name="answer[4]" value="B">   B  <input type="radio" name="answer[4]" value="C">   C   <input type="radio" name="answer[4]" value="D">  D
</td>
</tr>
<tr>
<td>5      <input type="radio" name="answer[5]" value="A">   A   <input type="radio" name="answer[5]" value="B">   B  <input type="radio" name="answer[5]" value="C">   C   <input type="radio" name="answer[5]" value="D">  D
</td>
</tr>
回声上的 php varibale 显示 Null..$tq是问题总数...
if(isset($_REQUEST['Submit']))
{ $x=1;
while($tq>=$x) {
echo "hiii";
$answer_id[] = $_POST['answer[]'] ;$x++;
var_dump( $answer_id[$x]);
}}
<form action="" method="post" enctype="multipart/form-data" name="form1" onsubmit="javascript:return valpass(this);">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr >
<td class="page-top-outer" ><?php include("header_content.php"); ?></td>
</tr>
<tr>
<td height="500" class="top-inp"><?php include("menu.php"); ?></td>
</tr>
<?php $x=0;
while($tq>0) { $tq--; $x++;
echo '<tr><td>'.$x.'   <input type="radio" name="answer['.$x.']" value="A">  A  <input type="radio" name="answer['.$x.']" value="B">  B  <input type="radio" name="answer['.$x.']" value="C">  C <input type="radio" name="answer['.$x.']" value="D">  D</td></tr>';
}
?>
<input name="Submit" type="Submit" value="Submit">
</table>
如果 $tq=5,则输出为 5 倍空。
$_POST
会像 -
array(
answer => array(
1 => value1,
2 => value2,
.......
)
)
尝试使用 -
$answers = $_POST['answer'];
foreach ($answers as $answer) {
var_dump($answer);
}
你能像这样更新代码并尝试:
.HTML
<tr>
<td>1      <input type="radio" name="answer1[]" value="A">   A  
<input type="radio" name="answer1[]" value="B">   B  
<input type="radio" name="answer1[]" value="C">   C  
<input type="radio" name="answer1[]" value="D">  D
</td>
</tr>
<tr>
<td>2      <input type="radio" name="answer2[]" value="A">   A  
<input type="radio" name="answer2[]" value="B">   B  
<input type="radio" name="answer2[]" value="C">   C  
<input type="radio" name="answer2[]" value="D">  D
</td>
</tr>
<tr>
<td>3      <input type="radio" name="answer3[]" value="A">   A  
<input type="radio" name="answer3[]" value="B">   B  
<input type="radio" name="answer3[]" value="C">   C  
<input type="radio" name="answer3[]" value="D">  D
</td>
</tr>
<tr>
<td>4      <input type="radio" name="answer4[]" value="A">   A  
<input type="radio" name="answer4[]" value="B">   B  
<input type="radio" name="answer4[]" value="C">   C  
<input type="radio" name="answer4[]" value="D">  D
</td>
</tr>
<tr>
<td>5      <input type="radio" name="answer5[]" value="A">   A  
<input type="radio" name="answer5[]" value="B">   B  
<input type="radio" name="answer5[]" value="C">   C  
<input type="radio" name="answer5[]" value="D">  D
</td>
</tr>
.PHP
$answer1 = $_POST['answer1'];
$answer2 = $_POST['answer2'];
$answer3 = $_POST['answer3'];
$answer4 = $_POST['answer4'];
$answer5 = $_POST['answer5'];