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.

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

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)