Harmiih真的幫我用我的腳本(http://stackoverflow.com/questions/7510546/html-form-name-php-variable)。基本上我有一張有問題的桌子。表單從表格中檢索問題,然後我需要選擇的答案轉到answer.php。一切工作與無線電按鈕,但與複選框它只發送最後選中的複選框。有人可以幫幫我嗎?形式與單選按鈕和複選框發送到PHP
形式
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' AND q_type = 'mr' ORDER BY RAND() LIMIT 5";
$result1=mysql_query($sql1);
echo "<form method='post' action='answer.php'>";
while($row1 = mysql_fetch_array($result1))
{
$test_name=$row1['test_name'];
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];
echo "<P><strong>$q_nr $question</strong><BR>";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question[$q_nr]' value='$option1'>$option1<BR>";
} else {
echo '';
}
if($option2!="") {
echo "<input type='radio' name='question[$q_nr]' value='$option2'>$option2<BR>";
} else {
echo '';
}
} else {
if($option1!="") {
echo "<input type='checkbox' name='question[$q_nr]' value='$option1'>$option1<BR>";
} else {
echo '';
}
if($option2!="") {
echo "<input type='checkbox' name='question[$q_nr]' value='$option2'>$option2<BR>";
} else {
echo '';
}
}
echo "<BR>";
echo "<BR>";
echo "</p>";
}
echo "<input type='submit' value='Send Form'>";
echo "</form>";
answer.php
<?php
//Key is $q_nr and $answer is selected $option
foreach($_POST['question'] as $key => $answer) {
echo $key;
echo $answer;
}
?>
謝謝JNDPNT的回答幫我,在我的形式我用: 名稱= '的問題[$ q_nr] []' 和我answer.php: 的foreach($ _ POST [ '問題'] as $ key => $ answer){ echo $ answer [0]; echo $ answer [1]; } – Wilest
順便說一下,你不必手動回覆所有的複選框答案,因爲你不知道用戶將選擇多少複選框。一個例子,你可以做到這一點。 [鏈接](http://pastebin.com/vjS44ZMi) – Harmiih
@Harmiih謝謝,現在就試試吧。 – Wilest