2015-04-30 60 views
0

的問題是我無法從HTML數組PHP數組變量後輸入。郵政單選按鈕數組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&nbsp &nbsp &nbsp <input type="radio" name="answer[1]" value="A"> &nbsp A &nbsp <input type="radio" name="answer[1]" value="B"> &nbsp B &nbsp<input type="radio" name="answer[1]" value="C"> &nbsp C &nbsp <input type="radio" name="answer[1]" value="D">&nbsp D 
    </td> 
</tr> 
<tr> 
    <td>2&nbsp &nbsp &nbsp <input type="radio" name="answer[2]" value="A"> &nbsp A &nbsp <input type="radio" name="answer[2]" value="B"> &nbsp B &nbsp<input type="radio" name="answer[2]" value="C"> &nbsp C &nbsp <input type="radio" name="answer[2]" value="D">&nbsp D 
    </td> 
</tr> 
<tr> 
    <td>3&nbsp &nbsp &nbsp <input type="radio" name="answer[3]" value="A"> &nbsp A &nbsp <input type="radio" name="answer[3]" value="B"> &nbsp B &nbsp<input type="radio" name="answer[3]" value="C"> &nbsp C &nbsp <input type="radio" name="answer[3]" value="D">&nbsp D 
    </td> 
    </tr> 
<tr> 
    <td>4&nbsp &nbsp &nbsp <input type="radio" name="answer[4]" value="A"> &nbsp A &nbsp <input type="radio" name="answer[4]" value="B"> &nbsp B &nbsp<input type="radio" name="answer[4]" value="C"> &nbsp C &nbsp <input type="radio" name="answer[4]" value="D">&nbsp D 
    </td> 
</tr> 
<tr> 
    <td>5&nbsp &nbsp &nbsp <input type="radio" name="answer[5]" value="A"> &nbsp A &nbsp <input type="radio" name="answer[5]" value="B"> &nbsp B &nbsp<input type="radio" name="answer[5]" value="C"> &nbsp C &nbsp <input type="radio" name="answer[5]" value="D">&nbsp D 
    </td> 
     </tr> 
回聲

腓varibale顯示空.. $ 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.'&nbsp&nbsp&nbsp<input type="radio" name="answer['.$x.']" value="A">&nbsp A &nbsp<input type="radio" name="answer['.$x.']" value="B">&nbsp B &nbsp<input type="radio" name="answer['.$x.']" value="C">&nbsp C&nbsp<input type="radio" name="answer['.$x.']" value="D">&nbsp D</td></tr>'; 

} 
?> 
<input name="Submit" type="Submit" value="Submit"> 
</table> 

輸出的5倍,如果空$ TQ = 5。

回答

2

$_POST會像 -

array(
    answer => array(
       1 => value1, 
       2 => value2, 
       ....... 
      ) 
) 

與嘗試 -

$answers = $_POST['answer']; 
foreach ($answers as $answer) { 
    var_dump($answer); 
} 
+0

試過輸出時TQ是4:串(1) 「A」 的字符串(1) 「C」 串(1) 「d」 串(1) 「d」 – user995426

+0

有了這個你甚至不需要'$ tq'作爲'foreach'將循環到最後一個元素。 –

+0

是的,雖然不需要,謝謝你。現在,如何存儲這個值作爲數組中的表列「正確答」 ...... 我試圖插入但在表中它顯示「數組」的值插入.. – user995426

0

可以更新這樣的代碼,並嘗試:

HTML

<tr> 
    <td>1&nbsp &nbsp &nbsp <input type="radio" name="answer1[]" value="A"> &nbsp A &nbsp 
     <input type="radio" name="answer1[]" value="B"> &nbsp B &nbsp 
     <input type="radio" name="answer1[]" value="C"> &nbsp C &nbsp 
     <input type="radio" name="answer1[]" value="D">&nbsp D 
    </td> 
</tr> 
<tr> 
    <td>2&nbsp &nbsp &nbsp <input type="radio" name="answer2[]" value="A"> &nbsp A &nbsp 
     <input type="radio" name="answer2[]" value="B"> &nbsp B &nbsp 
     <input type="radio" name="answer2[]" value="C"> &nbsp C &nbsp 
     <input type="radio" name="answer2[]" value="D">&nbsp D 
    </td> 
</tr> 
<tr> 
    <td>3&nbsp &nbsp &nbsp <input type="radio" name="answer3[]" value="A"> &nbsp A &nbsp 
     <input type="radio" name="answer3[]" value="B"> &nbsp B &nbsp 
     <input type="radio" name="answer3[]" value="C"> &nbsp C &nbsp 
     <input type="radio" name="answer3[]" value="D">&nbsp D 
    </td> 
    </tr> 
<tr> 
    <td>4&nbsp &nbsp &nbsp <input type="radio" name="answer4[]" value="A"> &nbsp A &nbsp 
     <input type="radio" name="answer4[]" value="B"> &nbsp B &nbsp 
     <input type="radio" name="answer4[]" value="C"> &nbsp C &nbsp 
     <input type="radio" name="answer4[]" value="D">&nbsp D 
    </td> 
</tr> 
<tr> 
    <td>5&nbsp &nbsp &nbsp <input type="radio" name="answer5[]" value="A"> &nbsp A &nbsp 
     <input type="radio" name="answer5[]" value="B"> &nbsp B &nbsp 
     <input type="radio" name="answer5[]" value="C"> &nbsp C &nbsp 
     <input type="radio" name="answer5[]" value="D">&nbsp D 
    </td> 
     </tr> 

PHP

$answer1 = $_POST['answer1']; 
$answer2 = $_POST['answer2']; 
$answer3 = $_POST['answer3']; 
$answer4 = $_POST['answer4']; 
$answer5 = $_POST['answer5'];