简体   繁体   English

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

[英]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.

相关问题 我如何使用while循环通过jQuery获取输入字段值 - How can i get input field value using while loop through jquery 如何获得每个数组的值,但有限制,我在while和if循环中使用php来使用它? - How to get every array value, but with the limitation that I am using it inside while and if loop using php? 我通过使用post方法提交表单,但是我无法从输入字段中获取值。 我如何获得价值 - I am submitting form by using post method but i am unable to get the value from input field. How I can get the value 如何使用PHP中的foreach循环从数组访问单个键和值? - How can I access an individual key and value from an array using a foreach loop in PHP? 为什么在使用 while 循环时出现“尝试访问 bool 类型值的数组偏移量”? - Why am I getting "Trying to access array offset on value of type bool" when using a while loop? 在使用SELECT SUM()GROUP BY时,如何仍能获得各个值? - While using a SELECT SUM() GROUP BY, how can I still get the individual values? 如何使用jquery获取php中文本字段和隐藏字段的循环值 - how can i get the value of the loop both text fields and hidden fields in php using jquery 如何在等待用户输入时暂停jQuery .each()循环? - How can I pause a jQuery .each() loop, while waiting for user input? 在PHP中如何为while循环中创建的每个输入文本框生成动态ID - in php how can i generate dynamic id for each input textbox created in while loop 使用循环选中复选框时,如何显示值? - How can i show value when checkbox was checked using loop for?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM