繁体   English   中英

使用while循环时,如何获取每个输入文本的单独值?

[英]How can I get the individual value of each input text when I am using a while loop?

我试图创建一个表,该表将提供项目列表,然后用户可以勾选想要输入的项目,然后输入想要的项目数量。

<table name="item_orders">
<thead>
    <tr>
        <th></th>
        <th>Item</th>
        <th>Price</th>
        <th>Quantity</th>
    </tr>
</thead>
<?php   
    $list=getItemList($connect);
    while($row=mysqli_fetch_assoc($list)){
?>
    <tr>
        <td><input type="checkbox"  name="check_list[]" value="<?=$row['id']?>"/></td>
        <td><?=$row['item']?></td>
        <td><?=$row['price']?></td>
        <td><input type="text" placeholder="How Many" name="howmany"/></td>
        </tr>   
<?php   
    }
?>
</table>

然后我将使用此代码保存它

if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) {
    $listy = getList($connect, $check) -> fetch_assoc();
    $totalamount = $listy['price'] * $howmany;
    addBalanceDB($connect, $userID, $listy['item'], $totalamount, null);
    }
}

问题是,如果用户选择2个项目((狗食200美元)和(猫食250美元)),然后将数量5作为狗食,将10作为猫食。 它只会得到10的值。 制作狗粮$ 2000,而不仅仅是$ 1000

您只需要在名称中添加方括号即可,使其成为数组:

<input type="text" placeholder="How Many" name="howmany[]"/>

还请确保添加一个隐藏值以标识给定索引处的当前项目:

<input type="hidden" value="<?=$row['id']?>" name="whatis[]"/>

然后将结果放入$_POST['howmany']$_POST['whatis']

另外,您也可以设置固定索引或单个名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM