为什么在输入元素的name属性中使用括号? [英] Why do I use brackets in the name attribute of input element?

查看:140
本文介绍了为什么在输入元素的name属性中使用括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在隐藏的HTML输入字段中使用方括号

Possible Duplicate:
Using square brackets in hidden HTML input fields

输入元素的name属性中的方括号怎么办? 这是教程:

What can I do with brackets in the name attribute of input element? This is tutorial:

<input id="user_email" name="user[email]" size="30" tabindex="1" type="text">

推荐答案

它们在HTML中没有特殊含义.一些服务器端框架(包括PHP)使用这种表示法表示它们应在单个服务器端对象(在PHP的情况下为关联数组)中建立数据.因此,例如,对于PHP,如果您拥有name="user[email]"name="user[phone]"并提交了表单,则在服务器上的PHP代码中,您将从请求中检索单个user对象,并且该对象的键为phone.或者,如果您在多个输入上使用name="tags[]",PHP将使用请求对象上所有名为tags的值来构建一个数组.

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.

这篇关于为什么在输入元素的name属性中使用括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆