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
Notice: Unknown: Skipping Numeric Key 1 in Unknown on Line 0
Composer Killed While Updating
How to Make Pdo Run Set Names Utf8 Each Time I Connect, in Zendframework
PHP Conditionals, Brackets Needed
PHP Daylight Saving Time Detection
Track When User Hits Back Button on the Browser
PHP 7.4 Deprecated Get_Magic_Quotes_Gpc Function Alternative
How to Open a Link in Private Browsing Mode
PHP Check Value Against Multiple Values with Or-Operator
Using Square Brackets in Hidden HTML Input Fields
How to Force Users to Access My Page Over Https Instead of Http
Disabling Strict Standards in PHP 5.4
How to Get the Unicode Value of a Character or Vise Versa with PHP
Is My Understanding of PHP Sessions Correct
How Get All Values in a Column Using PHP
Adding Custom Field to Product Category
Jquery - Uncaught Typeerror: Cannot Use 'In' Operator to Search for '324' In