php PHP如何读取输入字段的id?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4500164/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:13:05  来源:igfitidea点击:

How can an id of input field can be read by PHP?

phphtmlinput

提问by OM The Eternity

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.

我有一个 HTML 输入字段,并且每个输入字段都指定了一些 id,现在我如何使用这个输入字段 id 来标识该字段。

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.

我需要验证输入字段的 id 为 float,然后将输入字段的值插入到特定字段的 db 中。

Please help me out..

请帮帮我..

回答by deceze

You can't. When POSTing data to the server, the nameattribute is used. The idhas nothing much to do with PHP.

你不能。将数据 POST 到服务器时,将name使用该属性。该id有没有什么做用PHP。

回答by Felix Kling

It depends which method you use for transferring the data (specified by the methodattribute of the formelement).

这取决于您使用哪种方法传输数据(由元素的method属性指定form)。

E.g. with:

例如:

<form method="POST">
    <input type='text' id='float' name='attributename' value='' maxlength='30'/>
    <input type="submit" />
</form>

you can access the data in PHP via $_POST['attributename']when the form is submitted. $_POSTis an associative array that holds the data send via a POST request.

您可以在$_POST['attributename']提交表单时访问 PHP 中的数据。$_POST是一个关联数组,用于保存通过 POST 请求发送的数据。

Note:The nameof the input element is the important property, not the ID. The ID is nottransferred to the server, it plays only a role in the DOM. Only the nameand the valueis send to the server. So if you probably want to set name="float".

注:nameinput元素是最重要的财产,而不是ID。ID不会传输到服务器,它仅在 DOM 中发挥作用。只有namevalue被发送到服务器。所以如果你可能想设置name="float".

Further information:Variables From External Sources. You also might want to read about $_POSTand $_GET(GET is the other method. This is used to send data via the URL).

更多信息:来自外部来源的变量。您可能还想了解$_POST$_GET(GET 是另一种方法。它用于通过 URL 发送数据)。

回答by Surreal Dreams

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.

当 PHP 通过 GET 或 POST 获取数据时,它不知道与给定数据相关联的输入的 ID。然而,你知道这个名字。

The simplest approach is to make the name and id match, or be able to derive the id from the name, like so:

最简单的方法是让 name 和 id 匹配,或者能够从 name 派生 id,如下所示:

name:  float
id: float_id

name: address
id: address_id

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.

原因是 DOM 使用了 id,但它与 PHP 的执行无关。PHP 只获取名称。由于您可以控制服务器端和客户端代码,因此您可以通过选择我上面建议的命名约定来确定 id 是什么。

回答by Sarfraz

First of all, an idshould be unique for each element.

首先,id每个元素的an应该是唯一的。

You can suffix field name with []to create an arrayso that you can process them later like:

您可以为字段名称添加后缀[]以创建一个数组,以便您稍后可以像这样处理它们:

<input type='text' id='float1' name='attributename[]' value='' maxlength='30'/>
<input type='text' id='float2' name='attributename[]' value='' maxlength='30'/>
<input type='text' id='float3' name='attributename[]' value='' maxlength='30'/>

Now from PHP, you can use foreachto access each field value like this:

现在在 PHP 中,您可以foreach像这样访问每个字段值:

foreach($_POST['attributename'] as $value){
  echo $value . '<br>';
}

If it is a single field though, you can access it just by its name:

如果它是单个字段,则可以仅通过其名称访问它:

echo $_POST['attributename'];

回答by Matt Asbury

When you submit the form you would use the input name=""attribute to pull the value from:

当您提交表单时,您将使用 inputname=""属性从以下位置提取值:

$_POST['attributename'];

回答by Spiny Norman

In PHP, you only have access to the nameof the input, after the form has been submitted using method="get"or method="post", through $_GET['attributename']and $_POST['attributename'], respectively.

在 PHP 中,您只能在分别name使用method="get"method="post"、通过$_GET['attributename']和提交表单后才能访问输入的$_POST['attributename']

Maybe you mean to target the field using javascript; you can do that by using document.getElementById('float').

也许您的意思是使用 javascript 定位该领域;你可以通过使用来做到这一点document.getElementById('float')

回答by AutomationNation

Yes, deceze put it best.

是的,deceze 说得最好。

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.

基本上,PHP 中的所有内容(以及所有 Web 编程)都需要键 = 值对。所以,如果你没有钥匙,你也就没有价值。如果表单元素只有一个 ID 而没有 NAME,那么传递给 PHP 的变量将不存在。

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 $_GETinstead of $_POSTto pull the variables.

最好的测试方法是使用“GET”而不是“POST”来测试您的 HTML 表单元素,这样,当表单提交给方法时,您将在地址栏中看到键=值对那个行动。然后你必须做的就是使用$_GET而不是$_POST拉取变量。