duanjiong5686 2013-01-30 17:28
浏览 48
已采纳

PHP代码循环遍历表的行并发布文本/无线电输入

I would like to write PHP code that loops through rows of a table and posts both text input and a radio selection from each row.

Example HTML

  <body>
    <form method="post" action="table.php">
    <table>
      <tr>
        <td><input type="text" name="name[]"></td>
        <td><input type="radio" name="radio1" value="+">+</td>
        <td><input type="radio" name="radio1" value="-">-</td>
      </tr>
      <tr>
        <td><input type="text" name="name[]"></td>
        <td><input type="radio" name="radio2" value="+">+</td>
        <td><input type="radio" name="radio2" value="-">-</td>
      </tr>
      <tr>
        <td><input type="text" name="name[]"></td>
        <td><input type="radio" name="radio3" value="+">+</td>
        <td><input type="radio" name="radio3" value="-">-</td>
      </tr>
      <tr>
        <td><input type="text" name="name[]"></td>
        <td><input type="radio" name="radio4" value="+">+</td>
        <td><input type="radio" name="radio4" value="-">-</td>
      </tr>
      <tr>
        <td><input type="text" name="name[]"></td>
        <td><input type="radio" name="radio5" value="+">+</td>
        <td><input type="radio" name="radio5" value="-">-</td>
     </tr>
  </table>
  <input type="submit" value="submit">
  </form>
  </body>
</html>

in the case where the number of rows may vary.

  • 写回答

1条回答 默认 最新

  • douluhaikao93943 2013-01-30 17:29
    关注
    <?php
      $rowCount = count($_POST['name']);
      echo "<table>";
      for ($i=0; $i<$rowCount;$i++)
      {
        echo "<tr>";
        echo "<td>".$_POST['name'][$i-1]."</td>";
        echo "<td>".$_POST['radio'.$i]."</td>";
        echo "</tr>";
      }
    
      echo "</table>"
    ?>
    

    Get the number of rows by using count() to count an array element present in each row, in this case name="name[]". Then loop through the rows by creating a for loop and inside the for loop get each variable with $_POST. To get the radio value that was selected for each row (i.e. radio1, radio2, etc.), reference the element name not by array but just by name, because if you create an element radio[] different rows will be considered part of the same radio selection group.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大