简体   繁体   中英

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. It would only get the 10 value. Making dog food $2000 instead of just $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']

As an alternative you could make fixed indices or individual names of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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