'How can I use a number input to make an array?
edit: Somebody in the answers recommended a list, and I think that that is a good idea, but then I have no idea of how to implement that with the amount as well as the type. So if someone could explain that?
I have to make a fast food order form for school, and I decided to make you able to enter the amount as an input instead of checkboxes, but I still want them stored in an array. Is there any way to do that? Here's a little snippet of what I have in html:
<form>
<table class="menu" style="width:100%;">
<tr>
<td class="productlist">
<!-- Drinks -->
<input type="number" name="drinks[]" value="cokeclassic" id="cokeclassic" class="productCheckBox" min="0" max="20" placeholder="0">
<label for="cokeclassic" class="product">Coca Cola Classic $0.75<br><br></label>
</tr>
</table>
<input type="submit" value="Cart" class="submit">
</form>
Solution 1:[1]
If you want them stored in an array, you would need to have multiple input fields with name="drinks[]". All of your inputs will be sent as one array called drinks, containing numbers passed in each field.
If you want to pass name of a drink and an amount of it, you would need something like name="drinks[0].name" for name of your drink and name="drinks[0].amount" for amount
Solution 2:[2]
Why you would like to store them in an array?
When submitting the form, the input-tag can only deliver a single value anyways, because there is only a single input.
Try to do a list/stack with the orders or even try to create a dynamic multiselect including amounts when ordering.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Micha? Testka |
| Solution 2 | Jonas Oldenkott |
