PHP 如何读取输入字段的 id?
我有一个 HTML 输入字段,并且每个输入字段都指定了某个 id,现在如何使用此输入字段 id 来标识该字段。
示例:
<input type='text' id='float' name='attributename' value='' maxlength='30'/>
我需要验证 float 输入字段的 id,然后将输入字段的值插入到特定字段的 db 中。
请帮帮我..
I have an HTML input field, and I have every input field specified with some id, now how can I use this input field id to identify the field.
Example:
<input type='text' id='float' name='attributename' value='' maxlength='30'/>
I need to verify the id of input field for float and then insert the input field's value in to db in particular field.
Please help me out..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你不能。将数据 POST 到服务器时,将使用
name
属性。id
与 PHP 没有太大关系。You can't. When POSTing data to the server, the
name
attribute is used. Theid
has nothing much to do with PHP.这取决于您使用哪种方法传输数据(由
form
元素的method
属性指定)。例如:
提交表单时,您可以通过
$_POST['attributename']
访问 PHP 中的数据。$_POST
是一个关联数组,用于保存通过 POST 请求发送的数据。注意:输入元素的
名称
是重要的属性,而不是ID。 ID不会传输到服务器,它仅在 DOM 中发挥作用。仅将名称
和值
发送到服务器。因此,如果您可能想设置name="float"
。更多信息: 来自外部源的变量< /em>。您可能还想阅读有关
$_POST
和$_GET
(GET 是另一种方法。它用于通过 URL 发送数据)。It depends which method you use for transferring the data (specified by the
method
attribute of theform
element).E.g. with:
you can access the data in PHP via
$_POST['attributename']
when the form is submitted.$_POST
is an associative array that holds the data send via a POST request.Note: The
name
of the input element is the important property, not the ID. The ID is not transferred to the server, it plays only a role in the DOM. Only thename
and thevalue
is send to the server. So if you probably want to setname="float"
.Further information: Variables From External Sources. You also might want to read about
$_POST
and$_GET
(GET is the other method. This is used to send data via the URL).当 PHP 通过 GET 或 POST 获取数据时,它不知道与给定数据片段关联的输入的 id。不过,你确实知道这个名字。
最简单的方法是使名称和 id 匹配,或者能够从名称导出 id,如下所示:
原因是 DOM 使用 id,但它与 PHP 的执行无关。 PHP 只获取名称。由于您可以控制服务器端和客户端代码,因此您可以通过选择我上面建议的命名约定来确定 id。
When PHP gets the data by GET or POST, it doesn't know the id of the input that was associated with a given piece of data. You do, however, know the name.
The simplest approach is to make the name and id match, or be able to derive the id from the name, like so:
The reason is that the DOM uses the id, but it has nothing to do with PHP's execution. PHP only gets the name. Since you have control of both server-side and client-side code, you can determine what the id will be by choosing a naming convention as I suggested above.
当您提交表单时,您将使用输入
name=""
属性从以下位置提取值:When you submit the form you would use the input
name=""
attribute to pull the value from:首先,每个元素的
id
应该是唯一的。您可以在字段名称后添加
[]
来创建 数组,以便稍后可以处理它们,如下所示:现在,在 PHP 中,您可以使用
foreach
像这样访问每个字段值:如果它是单个字段,则可以仅通过其名称来访问它:
First of all, an
id
should be unique for each element.You can suffix field name with
[]
to create an array so that you can process them later like:Now from PHP, you can use
foreach
to access each field value like this:If it is a single field though, you can access it just by its name:
在 PHP 中,在使用
method="get"
或method="post"< 提交表单后,您只能访问输入的
name
/code>,分别通过$_GET['attributename']
和$_POST['attributename']
。也许您的意思是使用 javascript 定位该字段;您可以使用
document.getElementById('float')
来做到这一点。In PHP, you only have access to the
name
of the input, after the form has been submitted usingmethod="get"
ormethod="post"
, through$_GET['attributename']
and$_POST['attributename']
, respectively.Maybe you mean to target the field using javascript; you can do that by using
document.getElementById('float')
.是的,德塞说得最好。
基本上,PHP 中的所有内容(以及所有 Web 编程)都需要键 = 值对。所以,如果你没有钥匙,你也就没有价值。如果表单元素只有 ID 而没有 NAME,则传递给 PHP 的变量将不存在。
最好的测试方法是使用“GET”而不是“POST”来测试 HTML 表单元素,这样,当表单提交到方法时,您将在地址栏中看到 key=value 对的行动。然后您所要做的就是使用
$_GET
而不是$_POST
来提取变量。Yes, deceze put it best.
Basically, everything in PHP (and all of web programming) requires key = value pairs. So, if you have no key, you in turn have no value. If the form element has only an ID and no NAME, then the variable that is passed to PHP will not exist.
The best way to test, is to have your HTML form element test with a "GET" instead of a "POST", this way, you will see the key=value pairs in the address bar when the form is submitted to the method by the action. All then you must do is use
$_GET
instead of$_POST
to pull the variables.