Reputation: 91
The problem is I am not able to post inputs from HTML array to php array variable .
X radio button group say answer[1] ,answer[2] .... answer[x]. answer[1] is having four values A,B,C,D selected radio button value to be stored in answer[1] , same for all answer[2], .... answer[x]. So basically I want to store each selected option in array i.e. correct answer for 1 at array location 1 and so on... I am trying to get answers for objective questions and save it to array variable on submit and then insert in database in single column.
When user enters No. of questions (say 10) he gets A, B, C, D radio button for each question i.e. 10 times A,B,C,D and can select correct answer and submit. THen save all answers in array and insert in table .
Html output shows as...
<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 on echo shows Null.. $tq is total number of questions...
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>
Output is 5 times null if $tq=5.
Upvotes: 0
Views: 8069
Reputation: 4749
Can you update the code like this and try:
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'];
Upvotes: 0
Reputation: 31749
The $_POST
will be like -
array(
answer => array(
1 => value1,
2 => value2,
.......
)
)
Try with -
$answers = $_POST['answer'];
foreach ($answers as $answer) {
var_dump($answer);
}
Upvotes: 2