user995426
user995426

Reputation: 91

Post radio button array to php array variable

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&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> 

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.'&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>

Output is 5 times null if $tq=5.

Upvotes: 0

Views: 8069

Answers (2)

Sanjay Kumar N S
Sanjay Kumar N S

Reputation: 4749

Can you update the code like this and try:

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'];

Upvotes: 0

Sougata Bose
Sougata Bose

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

Related Questions