如何在PHP中一起使用复选框和输入类型编号

时间:2018-11-27 12:26:24

标签: php html checkbox

我正在使用php创建会议室预订应用程序。这样,当一个人预定房间时,可以选择预定茶或点心并指定他们需要的物品数量。为此,我使用复选框选择项目并输入类型编号以指定项目数量。即,如果选择了茶,则必须指定编号。输入字段中的茶我的问题是我无法同时存储或显示复选框值和关联的数字。我的代码是

<form class="form-horizontal" method="post" id="bookroom" action="bookedreview.php">
<h4>Food & Beverages</h4>
<div class="checkbox">
 <input type="checkbox" value="tea" name="food[]"><label>Tea</label>
 <input type="number" name="foodnum[]">
</div>
<div class="checkbox">
    <input type="checkbox" value="coffee" name="food[]"><label>Coffee</label>
    <input type="number" name="foodnum[]">
</div>
<div class="checkbox">
    <input type="checkbox" value="snacks" name="food[]"><label>Snacks</label>
    <input type="number" name="foodnum[]">
</div>
<div class="checkbox">
    <input type="checkbox" value="water" name="food[]"><label>Water</label>
    <input type="number" name="foodnum[]">
</div>
<div class="checkbox">
    <input type="checkbox" value="nuts" name="food[]"><label>Nuts</label>
    <input type="number" name="foodnum[]">
</div>
<div class="checkbox">
    <input type="checkbox" value="breakfast" name="food[]"><label>Breakfast</label>
    <input type="number" name="foodnum[]">
</div>
<div class="checkbox">
    <input type="checkbox" value="lunch" name="food[]"><label>Lunch</label>
    <input type="number" name="foodnum[]">
</div>
<div class="checkbox">
    <input type="checkbox" value="dinner" name="food[]"><label>Dinner</label>
    <input type="number" name="foodnum[]">
</div>
<input type="submit" name="submit" value="value">
</form>


if (isset($_POST['submit']))
{
  $foodnum=$_POST['foodnum'];
  $food=$_POST['food'];
  //$food=implode(',',$_POST['food']);
  foreach($foodnum as $index =>$value){
    $fud=$value;
    $num=$foodnum['index'];
 }
}

但是当我显示变量时,我无法获得结果。谁能帮助我将已检查项目和相关数量存储在一起。

1 个答案:

答案 0 :(得分:1)

您可以在文本框名称数组中传递食物名称

    <form class="form-horizontal" method="post" id="bookroom" action="">
    <h4>Food & Beverages</h4>
    <div class="checkbox">
     <input type="checkbox" value="tea" name="food[]"><label>Tea</label>
     <input type="number" name="foodnum[tea]">
    </div>
    <div class="checkbox">
        <input type="checkbox" value="coffee" name="food[]"><label>Coffee</label>
        <input type="number" name="foodnum[coffee]">
    </div>
    <div class="checkbox">
        <input type="checkbox" value="snacks" name="food[]"><label>Snacks</label>
        <input type="number" name="foodnum[snacks]">
    </div>
    <div class="checkbox">
        <input type="checkbox" value="water" name="food[]"><label>Water</label>
        <input type="number" name="foodnum[water]">
    </div>
    <div class="checkbox">
        <input type="checkbox" value="nuts" name="food[]"><label>Nuts</label>
        <input type="number" name="foodnum[nuts]">
    </div>
    <div class="checkbox">
        <input type="checkbox" value="breakfast" name="food[]"><label>Breakfast</label>
        <input type="number" name="foodnum[breakfast]">
    </div>
    <div class="checkbox">
        <input type="checkbox" value="lunch" name="food[]"><label>Lunch</label>
        <input type="number" name="foodnum[lunch]">
    </div>
    <div class="checkbox">
        <input type="checkbox" value="dinner" name="food[]"><label>Dinner</label>
        <input type="number" name="foodnum[dinner]">
    </div>
    <input type="submit" name="submit" value="value">
    </form>
    <?php

 if (isset($_POST['submit']))
{
    $foodnum=$_POST['foodnum'];
    $food=$_POST['food'];


      echo "<table border='1' style='width:100%'><tr><th>Food Name</th><th>Count</th></tr>";
    // output data of each row
   foreach($food as $foo)
    {
        echo "<tr><td>".$foo."</td><td>".$foodnum[$foo]."</td></tr>";
    }
    echo "</table>";
}

    ?>

用于插入

     foreach($food as $foo)
            {
$fieldVal1=$foo;
$fieldVal1=$foodnum[$foo];
        $query ="INSERT INTO foodcounts( foodsname, cnt) VALUES ('".$fieldVal1."','".$fieldVal2."' )";
        mysqli_query($conn, $query);
            }