[英]How can I get the individual value of each input text when I am using a while loop?
Im trying to create a table where it would give the list of items then the user can tick the items that they want the input the number of items they want. 我试图创建一个表,该表将提供项目列表,然后用户可以勾选想要输入的项目,然后输入想要的项目数量。
<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>
Then I would save it using this code 然后我将使用此代码保存它
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);
}
}
The problem is if the user would select 2 items like (dog food $200) and (cat food $250) then puts a quantity of 5 for the dog food and 10 for the cat food. 问题是,如果用户选择2个项目((狗食200美元)和(猫食250美元)),然后将数量5作为狗食,将10作为猫食。 It would only get the 10 value.
它只会得到10的值。 Making dog food $2000 instead of just $1000
制作狗粮$ 2000,而不仅仅是$ 1000
You just have to add brackets to your name, making it an array: 您只需要在名称中添加方括号即可,使其成为数组:
<input type="text" placeholder="How Many" name="howmany[]"/>
Also make sure to add a hidden value to identify the current item at that given index: 还请确保添加一个隐藏值以标识给定索引处的当前项目:
<input type="hidden" value="<?=$row['id']?>" name="whatis[]"/>
The results should then be put into $_POST['howmany']
and $_POST['whatis']
然后将结果放入
$_POST['howmany']
和$_POST['whatis']
As an alternative you could make fixed indices or individual names of course. 另外,您也可以设置固定索引或单个名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.