如何在按下php按钮时从多个值中获取输入值


How to get the input value from multiple value when button pressed php

我想做一辆手推车,我的产品是披萨。因此,我将所有菜单都列到表中,并添加一个按钮将菜单提交到购物车。

这是我的index.php

<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>

<form action="cart.php" method="get">
    <table  border="1" style="width:100%">
        <tr>
            <th>Pizza Name</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Buy</th>
        </tr>
        <?php while($product = mysqli_fetch_object($result)){ ?>
            <tr>
                <td><?php echo $product->nama_menu; ?></td>
                <td><?php echo $product->harga_menu; ?></td>
                <td><input type="number" name="quantity" min="1" max="20"></td>
                <td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
            </tr>
        <?php } ?>
    </table>
</form>     

但当我按下"添加到购物车"按钮时,它会发送菜单列表中的所有数量,并且无法在我的购物车中读取。php

当我按下"添加到购物车"按钮时,有人能帮我如何获得正确的数量值吗。

为每个项目制作单独的表格。请尝试以下代码。

<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>

    <table  border="1" style="width:100%">
        <tr>
            <th>Pizza Name</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Buy</th>
        </tr>
        <?php while($product = mysqli_fetch_object($result)){ ?>
            <form action="cart.php" method="get">
            <tr>
                <td><?php echo $product->nama_menu; ?></td>
                <td><?php echo $product->harga_menu; ?></td>
                <td><input type="number" name="quantity" min="1" max="20"></td>
                <td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
            </tr>
            </form>
        <?php } ?>
    </table>

尝试在名称中使用数组,然后提交它将给您单独的数量。

<td><input type="number" name="quantity[<?php echo $product->nama_menu; ?>]" min="1" max="20">

输出:数量['pizza1']=10数量['pizza2']=20……

另一种选择是使用动态名称作为数字。

<td><input type="number" name="quantity_<?php echo $product->nama_menu; ?>" min="1" max="20">

输出:quantity_pizza1=10quantity_pizza2=20…