php HTML 元素数组,name="something[]" 还是 name="something"?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4688880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML Element Array, name="something[]" or name="something"?
提问by Cheung
I saw something on this site : Handling array of HTML Form Elements in JavaScript and PHPhttp://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=343
我在这个网站上看到了一些东西: Handling array of HTML Form Elements in JavaScript and PHP http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=343
It said about put the array in name property and how the get the input collection's value.
e.g. name="education[]"
它说关于将数组放入 name 属性以及如何获取输入集合的值。例如name="education[]"
But as I know, HTML input element is array-ready by name.
On client-side (GetElementsByName) or server-side ($_POSTin PHP or Request.Formin ASP.NET)
for example: name="education", so what is the different with or with not the []?
但据我所知,HTML 输入元素是由name. 例如,在客户端 ( GetElementsByName) 或服务器端($_POST在 PHP 或Request.FormASP.NET 中):name="education",那么使用或不使用 有什么不同[]?
回答by Fenton
PHP uses the square bracket syntax to convert form inputs into an array, so when you use name="education[]"you will get an array when you do this:
PHP 使用方括号语法将表单输入转换为数组,所以当你使用时name="education[]"你会得到一个数组:
$educationValues = $_POST['education']; // Returns an array
print_r($educationValues); // Shows you all the values in the array
So for example:
例如:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]">
</p>
<p><label>Please enter any previous education<br>
<input type="text" name="education[]">
</p>
<p><label>Please enter any previous education<br>
<input type="text" name="education[]">
</p>
Will give you all entered values inside of the $_POST['education']array.
将为您提供$_POST['education']数组内所有输入的值。
In JavaScript, it is more efficient to get the element by id...
在 JavaScript 中,通过 id 获取元素更高效...
document.getElementById("education1");
The id doesn't have to match the name:
id 不必与名称匹配:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]" id="education1">
</p>
回答by sissonb
if you have check boxes you can pass an array of checked values.
如果您有复选框,则可以传递一组选中的值。
<input type="checkbox" name="fruits[]" value="orange"/>
<input type="checkbox" name="fruits[]" value="apple"/>
<input type="checkbox" name="fruits[]" value="banana"/>
Also multiple select dropdowns
还有多个选择下拉列表
<select name="fruits[]" multiple>
<option>apple</option>
<option>orange</option>
<option>pear</option>
</select>
回答by Galley
It's different. if you post this form:
这是不同的。如果您发布此表格:
<input type="text" name="education[]" value="1">
<input type="text" name="education[]" value="2">
<input type="text" name="education[]" value="3">
you will get an array in PHP, in this example you will get $_POST['education'] = [1, 2, 3].
你会得到一个 PHP 数组,在这个例子中你会得到$_POST['education'] = [1, 2, 3].
if you post this form without []:
如果您发布此表格而没有 []:
<input type="text" name="education" value="1">
<input type="text" name="education" value="2">
<input type="text" name="education" value="3">
you will get the last value, here you will get $_POST['education'] = 3.
您将获得最后一个值,在这里您将获得$_POST['education'] = 3.

