2017-05-09 51 views
-1

我有多个复选框,在我的我的网页之一腓 - 获取多个复选框,输入

<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; 
    } 
    } 

阵列我想用来存储所有的数据到二维数组。所以..

特殊[1] [0]是noNuts

特殊[1] [1]是noGluten

特殊[2] [0]是noNuts

特殊[2] [1]是noCorn

请帮帮我。先谢谢你!

+0

什么结果你现在得到些什么? –

+0

你似乎是沉默的类型,你给出的答案的接受记录似乎也反映了这一点。你来这里寻求帮助,给出了评论,要求澄清和答案,因为这也是不被接受的。如果你想保持良好的排名,你需要合作。这是一条双向的街道,而不是一条街道。 –

+0

目前我只能将所有特殊[]值填充到一个数组中。我不知道如何将它填充到2D数组中 –

回答

0

尝试修改此

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

$currentValue = $_POST['special']; 
-1

CONCAT在$我空间

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

在你的代码试图获得特殊的二维阵列,但在现实中,它is single 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]; 
    } 
    }?> 
+0

我试过这种方式。我能够将它存储在数组中,但当我调用var_dump时它将返回零。不管怎样,谢谢你 –

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 
       ) 

     ) 

)