【问题标题】:Post radio button array to php array variable将单选按钮数组发布到 php 数组变量
【发布时间】:2015-07-09 22:31:49
【问题描述】:

问题是我无法将输入从 HTML 数组发布到 php 数组变量。

X 单选按钮组说 answer[1] ,answer[2] .... answer[x]。 answer[1] 有四个值 A,B,C,D 选择的单选按钮值存储在 answer[1] 中,对于所有 answer[2], .... answer[x] 都相同。所以基本上我想将每个选定的选项存储在数组中,即数组位置 1 的 1 的正确答案等等...... 我正在尝试获取客观问题的答案并将其保存到提交时的数组变量中,然后在单列中插入数据库中。

当用户输入问题数(例如 10)时,他会为每个问题获得 A、B、C、D 单选按钮,即 A、B、C、D 的 10 次,并且可以选择正确答案并提交。 然后将所有答案保存在数组中并插入表中。

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> 

echo 上的 PHP 变量显示 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.'&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>

如果 $tq=5,则输出为 5 倍。

【问题讨论】:

    标签: php html arrays post


    【解决方案1】:

    你能不能像这样更新代码并尝试:

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

    【讨论】:

      【解决方案2】:

      $_POST 会像 -

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

      试试 -

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

      【讨论】:

      • tq 为 4 时的尝试输出为:string(1) "A" string(1) "C" string(1) "D" string(1) "D"
      • 使用这种方法你甚至不需要$tq 因为foreach 将循环到最后一个元素。
      • 是的,虽然不需要谢谢你。现在如何将此值作为数组存储在表列“正确的ans”中......我试图插入但在表中它显示“数组”作为插入的值..
      • 这将取决于您的表结构。虽然您可以在循环内运行查询并存储它们。
      • 如果你想保存序列化的数据,那么你为什么要运行循环。 ??只需序列化answer 并保存即可。
      猜你喜欢
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      相关资源
      最近更新 更多