I had a quick question about using forms in html. If you have an id attribute but no name attribute , does it affect it? I want to make sure I understood this properly. Because when I tested it in my program , I changed the name attribute to ID , and the program did not work right. It only worked right when I had the name attribute in there. So , then the answer to this question would be yes it does affect it. correct?
Asked
Active
Viewed 31 times
0
-
1[Writing the perfect question](http://tinyurl.com/stack-hints) – Andreas Oct 23 '15 at 08:09
-
You have to search a little bit before asking a question. The answer is already posted here http://stackoverflow.com/questions/1397592/difference-between-id-and-name-attributes-in-html – Kristijan Iliev Oct 23 '15 at 08:10
-
The form element doesn't need a name, but a form control (input, select, etc.) must have a name to be successful (i.e. be submitted with the form). – RobG Oct 23 '15 at 08:10
-
i read that previous link , i didnt quite understand it. I couldnt understand if one worked without the other , that why i asked here. – user3225981 Oct 23 '15 at 08:14
-
in the case of a form submission , then the name attribute must be present for it to go through correct? but you dont really need the id attribute? – user3225981 Oct 23 '15 at 08:15
-
@user3225981—correct. Also, form controls can be referenced by their name as named properties of the form, so they don't need an ID at all (and resolving properties of the form is very much more efficient than using a function to get them by ID). If you have a specific problem, post a minimal example and you will get much better answers. – RobG Oct 24 '15 at 00:49
1 Answers
0
Simple:
In a form:
<form ...>
<input id="xy" name="ab">
</form>
It's only possible to get the data via the name
.
e.g. $yourvar = $_POST["ab"]
However you can use jQuery
to get the value of the input
:
<script>
...
var xy = $("#xy").val();
...
</script>
In conclusion:
You need the name for form
action, it is not possible to do this without.

Sebastian Schneider
- 4,896
- 3
- 20
- 47
-
You certainly don't need jQuery for that and an ID is not required at all. – RobG Oct 23 '15 at 08:12
-
@RobG yes i know, you can simply use `JavaScript`, but `jQuery` is easier (simple example). If you want to have the js code i can add it too. – Sebastian Schneider Oct 23 '15 at 08:14
-
thanks guys, this answers my questions. in the case of a form submission , then the name attribute must be present for it to go through correct? but you dont really need the id attribute. is what i understand – user3225981 Oct 23 '15 at 08:16
-
@user3225981 thats true, you don't need `id` but need `name`. Please mark my answer as correct (and maybe upvote it) if it helps you ;) – Sebastian Schneider Oct 23 '15 at 08:18
-
i will , it wont let me mark it with in the first 4 mins lol . thank you for the help – user3225981 Oct 23 '15 at 08:18
-
-
@SeseSchneider—after working on a number of projects with jQuery and various addons and plugins, I think it leads to an unstable mass of unreliable code. e.g. even the "simple" `$("#xy").val()` now means adding an ID to an element that previously didn't need one and requires a 100kb library. – RobG Oct 24 '15 at 00:45