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

2019-01-19 08:51发布

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..

标签: php html input
7条回答
别忘想泡老子
2楼-- · 2019-01-19 09:10

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

查看更多
够拽才男人
3楼-- · 2019-01-19 09:13

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'];
查看更多
beautiful°
4楼-- · 2019-01-19 09:14

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.

查看更多
爷的心禁止访问
5楼-- · 2019-01-19 09:14

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').

查看更多
beautiful°
6楼-- · 2019-01-19 09:22

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

$_POST['attributename'];
查看更多
Luminary・发光体
7楼-- · 2019-01-19 09:23

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).

查看更多
登录 后发表回答