簡體   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