php - Assign an array to the 'name' attribute in an HTML form
问题描述
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.
解决方案
在您的代码的第一部分是如前所述item[]
。该数组从输入中一一收集数据,并以数字索引的形式对其进行排列。只是在 PHP 的情况下,索引被添加为键。
例如:如果我们在输入中选择多个项目(如 item1 和 item2),item[]
则将取值并将它们排列为:
array(
[0] => 'item1',
[1] => 'item2'
)
推荐阅读
- node.js - 当数据超过 80k 字节时截断 mssql 库中的 nvarchar
- flutter - 是否可以使用 SQFLITE 让用户在设备上保持登录状态?
- voice-recognition - 为什么在这个例子中,window_length/hop_length 与 librosa.core.stft 中的采样率相乘?
- java - Android webview chromium 异常 - ContextResult::kFatalFailure: 支持的制服或变量太少
- google-cloud-run - 为什么在云运行域映射中需要很长时间才能将我的服务映射到 GoDaddy 中的域?
- azure-ad-b2c - 如何测试 azure ad b2c 的自定义全局异常页面?
- php - 根据字符串开头的连续符号数创建分层多维数组
- dart - 过滤列表对象包含值
- javascript - 我如何使用箭头(按钮)导航 iframe 或 div 并像谷歌文档一样放大和缩小,但对于 html 页面?
- javascript - 无法从 Firestore 中检索数据