Using Square Brackets in Hidden HTML Input Fields

Using square brackets in hidden HTML input fields

Yes. Basically PHP will know to stick all of those values with the same name into an array.

This applies to all input fields, by the way, not just hidden ones.

Why do I use brackets in the name attribute of input element?

They have no particular meaning in HTML. Some server-side frameworks, including PHP, use that sort of notation as an indication that they should build up the data in a single server-side object (an associative array in PHP's case). So with PHP, for instance, if you had name="user[email]" and name="user[phone]" and submitted the form, in your PHP code on the server you'd retrieve a single user object from the request and it would have the keys email and phone on it. Or if you had name="tags[]" on multiple inputs, PHP would build an array with all of the values called tags on the request object.

target multiple empty input fields with same id containing brackets

The id in dom is always unique.Instead use document.querySelectorAll to get all the element with same name then loop through them and use the index to check if an element exist in the index of testArray. If so then set the value of the input

var testArray = ['hi', 'bye', 'stackoverflow'];document.querySelectorAll('[name="numberC[]"]').forEach(function(item, index) {  if (testArray[index]) {    item.value = testArray[index]  }
})
<input type="text" name="numberC[]" id="numberC[1]"><input type="text" name="numberC[]" id="numberC[2]"><input type="text" name="numberC[]" id="numberC[3]">

Backbone.js hidden fields on submit

Yeah, it's quite simple :)

<input type="hidden" name="item_name_<%= i %>" value="beach ball" />

FYI:

<%  i %> evaluate
<%= i %> interpolate
<%- i %> escape

HTML form with multiple hidden control elements of the same name

The browsers are OK with it. However, how the application library parses it may vary.

Programs are supposed to group identically named items together. While the HTML specification doesn't explicitly say this, it is implicitly stated in the documentation on checkboxes:

Several checkboxes in a form may share
the same control name. Thus, for
example, checkboxes allow users to
select several values for the same
property.



Related Topics



Leave a reply



Submit