我正在尝试创建一个表格,它将给出项目的列表,然后用户可以勾选他们想要的项目,输入他们想要的项目的数量。
<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美元
发布于 2015-03-01 16:27:59
你只需要在你的名字后面加上括号,使它成为一个数组:
<input type="text" placeholder="How Many" name="howmany[]"/>还要确保添加一个隐藏值,以标识该给定索引处的当前项:
<input type="hidden" value="<?=$row['id']?>" name="whatis[]"/>然后将结果放入$_POST['howmany']和$_POST['whatis']中
作为另一种选择,你可以建立固定的索引或单独的名字。
https://stackoverflow.com/questions/28791487
复制相似问题