当我点击“AddToCart”时,为什么我只能得到表格中最后一行的值?在另一排

时间:2018-01-06 18:47:11

标签: php html

我在php中有这段代码

<div style="width:75%;height: 600px;float: left;overflow-x: auto;">
        <form method="POST" action="addtocart.php">
            <table class="table" style="overflow-x: auto; width: 900px;">

            <tr>
                <th class="danger" id="th">Items</th>
                <th class="danger" id="th">Price(PhP)</th>
                <th class="danger" id="th">Quantity</th>
                <th class="danger" id="th">Action</th>
            </tr>

                <?php
                $item=mysqli_query($conn,"select * from store");
                while($pqrow=mysqli_fetch_array($item)){
                    $iid=$pqrow['id_item'];

                ?>
            <tr>
                <td class="warning" id="td">
                    <?php echo $pqrow['item']; ?></td>
                <td class="warning" id="td"><input type="hidden" name="price" value="<?php echo $pqrow['price']; ?>"><?php echo $pqrow['price']; ?></td>
                <td class="warning" id="td"><input type="number" style="width: 70px;" name="qty" value="1"></td>
                <td class="warning" id="td"><input type="hidden" name="item" value="<?php echo $iid; ?>"><input class="btn btn-success btn-md" type="submit" name="addtocart" value="AddToCart<?php echo $iid; ?>"></td>
            </tr><?php } ?>


        </table></form>
    </div>

当我在第一行中单击AddToCart时,最后一行的值已被提取。我想在第一行单击AddToCart时获取id和输入数量。

1 个答案:

答案 0 :(得分:0)

由于此表单上只有一个form标记和多个input具有相同名称,因此下一个输入的值将覆盖前一个。因此,始终具有最后一行的值。

您可以修复它,例如为每一行创建单独的表单,例如:

<div style="width:75%;height: 600px;float: left;overflow-x: auto;">
    <table class="table" style="overflow-x: auto; width: 900px;">
        <tr>
            <th class="danger" id="th">Items</th>
            <th class="danger" id="th">Price(PhP)</th>
            <th class="danger" id="th">Quantity</th>
            <th class="danger" id="th">Action</th>
        </tr>
    <?php
        $item = mysqli_query($conn,"select * from store");
        while ($pqrow=mysqli_fetch_array($item)) {
            $iid=$pqrow['id_item'];?>
        <tr>
            <td class="warning" id="td">
                <?php echo $pqrow['item']; ?></td>
            <td class="warning" id="td"><?php echo $pqrow['price']; ?></td>
            <td class="warning" id="td"><input type="number" style="width: 70px;" name="qty" value="1"></td>
            <td class="warning" id="td">
                <!-- Separate form for each record -->
                <form method="POST" action="addtocart.php">
                    <input type="hidden" name="price" value="<?php echo $pqrow['price']; ?>">
                    <input type="hidden" name="item" value="<?php echo $iid; ?>">
                    <input class="btn btn-success btn-md" type="submit" name="addtocart" value="AddToCart<?php echo $iid; ?>">
                </form>
            </td>
        </tr>
        <?php } ?>
    </table>
</div>

在这种情况下,您应该使用javascript跟踪数量字段的值。当然,您也可以在表单中移动<input type="number">,但这会破坏您的标记。

此外,如果您熟悉javascript,则可以添加一些客户端预处理(从某行的input获取值)并使用ajax-request将这些值发送到服务器。