可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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..
回答1:
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).
回答2:
You can't. When POSTing data to the server, the name
attribute is used. The id
has nothing much to do with PHP.
回答3:
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.
回答4:
When you submit the form you would use the input name=""
attribute to pull the value from:
$_POST['attributename'];
回答5:
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'];
回答6:
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')
.
回答7:
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.