Php - 获取多个复选框输入

时间:2017-05-09 11:32:33

标签: php html

我的页面中有多个复选框

<form method="POST" action="flight_cart.php" autocomplete="off">
<table>
    <?php
        for($i = 1; $i <= 5 ; $i++)
        {
            print "<tr>
                    <td><input class='seats' name='seats[$i]' type='checkbox' value=S".$i.">Seat " .$i. "</input></td>
                    <td><input class='special s$i' name='special[]'  type='checkbox' value='noNuts'>No Nuts</input></td>
                    <td><input class='special s$i' name='special[]'  type='checkbox' value='noGluten'>No Gluten</input></td>
                    <td><input class='special s$i' name='special[]'  type='checkbox' value='noCorn'>No corn</input></td>
                    </tr>";
        }
        print "<tr><td><button type='submit' onclick='return submitValidation();'>Add to Bookings</td></tr>";
    ?>
</table>

我想使用php获取下一页中的数据,如:

座位S1; NoNuts,NoGluten

座位S2; NoNuts,NoCorn

到目前为止,我只能获得一系列挑选座位

<?php

  $booked_seats = $_POST['seats'];
  $special = array();

  for($i = 0; $i < sizeof($booked_seats); $i++)
  {
    for($j = 0; $j < sizeof($_POST['special']); $j++)
    {
      $currentValue = $_POST['special'][$j];
      $special[$i][$j] = $currentValue;
    }
  }

我想将所有数据存储到2D数组中。所以..

特殊[1] [0]是noNuts

特殊[1] [1]是noGluten

特殊[2] [0]是noNuts

特殊[2] [1]是noCorn

请帮帮我。提前谢谢!

3 个答案:

答案 0 :(得分:0)

尝试更改此

$currentValue = $_POST['special'][$j];

$currentValue = $_POST['special'];

答案 1 :(得分:0)

试试这个。

echo "<form action='test.php' method='POST'>";
echo '<table>';
for($i = 1; $i <= 5 ; $i++)
{
    print "<tr>
            <td><input class='seats' name='special[$i]' type='checkbox' value=S".$i.">Seat " .$i. "</input></td>
            <td><input class='special s$i' name='special[$i][]'  type='checkbox' value='noNuts'>No Nuts</input></td>
            <td><input class='special s$i' name='special[$i][]'  type='checkbox' value='noGluten'>No Gluten</input></td>
            <td><input class='special s$i' name='special[$i][]'  type='checkbox' value='noCorn'>No corn</input></td>
            </tr>";
}
print "<tr><td><button type='submit'>Add to Bookings</td></tr>";
echo '</table>';
echo '</form>';

$ _ POST内容

Array
(
    [special] => Array
        (
            [1] => Array
                (
                    [0] => noNuts
                    [1] => noGluten
                    [2] => noCorn
                )

            [2] => Array
                (
                    [0] => noGluten
                    [1] => noCorn
                )

            [3] => Array
                (
                    [0] => noNuts
                    [1] => noGluten
                )

        )

)

答案 2 :(得分:-1)

以空格

连接$ i
<form method="POST" action="flight_cart.php" autocomplete="off">
<table>
    <?php
        for($i = 1; $i <= 5 ; $i++)
        {
            print "<tr>
                    <td><input class='seats' name='seats[$i]' type='checkbox' value=S".$i.">Seat " .$i. "</input></td>
                    <td><input class='special s$i' name='special.$i.[]'  type='checkbox' value='noNuts'>No Nuts</input></td>
                    <td><input class='special s$i' name='special.$i.[]'  type='checkbox' value='noGluten'>No Gluten</input></td>
                    <td><input class='special s$i' name='special.$i.[]'  type='checkbox' value='noCorn'>No corn</input></td>
                    </tr>";
        }
        print "<tr><td><button type='submit' onclick='return submitValidation();'>Add to Bookings</td></tr>";
    ?>
</table>

在您的代码中,您尝试获得特殊的2d数组,但实际上它是单个arrya ......

<?php
  $booked_seats = $_POST['seats'];
  $special = array();
  for($i = 0; $i < sizeof($booked_seats); $i++)
  {

    $currentValue = $_POST['special'.$i]; //concat $i to get perticular seat special.
    for($j = 0; $j < count($currentValue); $j++)
    {
      $special[$i][$j] = $currentValue.$i.[$j];
    }
  }?>