Home > Back-end >  How can I use a number input to make an array?
How can I use a number input to make an array?

Time:02-24

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  style="width:100%;">
    <tr>
      <td >
        <!-- Drinks -->
        <input type="number" name="drinks[]" value="cokeclassic" id="cokeclassic"  min="0" max="20" placeholder="0">
        <label for="cokeclassic" >Coca Cola Classic $0.75<br><br></label>
    </tr>
  </table>
  <input type="submit" value="Cart" >
</form>

CodePudding user response:

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

CodePudding user response:

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.

  • Related