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

发布于 2024-10-08 07:37:02 字数 251 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

懒猫 2024-10-15 07:37:02

你不能。将数据 POST 到服务器时,将使用 name 属性。 id 与 PHP 没有太大关系。

You can't. When POSTing data to the server, the name attribute is used. The id has nothing much to do with PHP.

你是年少的欢喜 2024-10-15 07:37:02

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

例如:

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

提交表单时,您可以通过 $_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 the form element).

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. $_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 the name and the value is send to the server. So if you probably want to set name="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).

2024-10-15 07:37:02

当 PHP 通过 GET 或 POST 获取数据时,它不知道与给定数据片段关联的输入的 id。不过,你确实知道这个名字。

最简单的方法是使名称和 id 匹配,或者能够从名称导出 id,如下所示:

name:  float
id: float_id

name: address
id: address_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:

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.

笑脸一如从前 2024-10-15 07:37:02

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

$_POST['attributename'];

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

$_POST['attributename'];
山田美奈子 2024-10-15 07:37:02

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

您可以在字段名称后添加 [] 来创建 数组,以便稍后可以处理它们,如下所示:

<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'/>

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

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

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

echo $_POST['attributename'];

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:

<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 foreach to access each field value like this:

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'];
┾廆蒐ゝ 2024-10-15 07:37:02

在 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 using method="get" or method="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').

べ繥欢鉨o。 2024-10-15 07:37:02

是的,德塞说得最好。

基本上,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文