1

I went through some code, and I found some code snippet as follows. Here I see, an array is assigned to name attributes like name="item[] and name="item[{0}]". I think this is an array assigning to add, items values again and again. I am not sure about that since I have not been using this before. Is this an array assignment or how is it used?

This is the first part in which the form displays a field to add items.

<input type="text" class="form-control item" name="item[]" required="required" placeholder=" Item Name">

The JavaScript code is there to add a new field to enter items once Add Another Item button clicks

<script type="text/html" id="addChild">
    <input type="text" class="form-control item" name="item[{0}]" required="required" placeholder="Item Name">
</script>

Once the button clicks this above section add a new field to add items.

3
  • 1
    What this does is store the values of the form (in this case item) in the form of array which can be accessed by server siide as an array (like PHP). You can print the POST global variable to check them out Commented Apr 29, 2020 at 5:16
  • 1
    Refer to my answer which is smiliar to this : stackoverflow.com/questions/53564665/… Commented Apr 29, 2020 at 5:19
  • @DhavalChheda, Thank you for the help:) Commented Apr 29, 2020 at 5:24

1 Answer 1

1

In the first part of your code is as mentioned item[]. This array collects the data from the input one by one and arranges it in numerically indexed form. It is just in case of PHP the indexes are added as keys.

For example: If we select multiple items (such as item1 and item2) in the input, the item[] will take the values and arrange them as:

array(
    [0] => 'item1',
    [1] => 'item2'
)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.