67

For efficiency purposes I am wondering if a file or text in a textarea still gets transferred to the server if you omit the name attribute or set it to null. eg

<input type="file" id="file" name="">
<textarea id="text" name="">

I notice that the data is not available at the server if you do this.

Jamie Kitson
  • 3,973
  • 4
  • 37
  • 50

3 Answers3

80

The W3C specification, if I understand it correctly, mandates that every form input element has a name attribute specified. Otherwise that element will not be processed. Source

Petter
  • 4,053
  • 27
  • 33
  • 32
    More exactly, only fields (“controls”) that have a `name` attribute contribute to the form data set. Without such an attribute, the field will still be processed normally, and the form can be submitted, just without data from that field. It is not an error to a have a field without a `name` attribute. – Jukka K. Korpela Sep 22 '12 at 17:55
  • 1
    Can someone quote the precise HTML5? – Ciro Santilli OurBigBook.com Jul 06 '14 at 16:52
  • 2
    @CiroSantilli六四事件法轮功纳米比亚威视 http://www.w3.org/TR/html5/forms.html#naming-form-controls:-the-name-attribute – Nick Humphrey Oct 07 '15 at 09:32
  • @NickHumphrey I don't have a lot of time to read it now, but after 1 minute I don't see the quote I want. I see it "cannot be empty", but not "what must be done if it is". If you find a direct quote or sequence of quotes, I suggest you put it in an answer and get rep :-) – Ciro Santilli OurBigBook.com Oct 07 '15 at 09:43
  • 5
    @CiroSantilli Very late response, but the specific information you want is actually under [Form Submission](https://www.w3.org/TR/html5/forms.html#constructing-form-data-set), particularly: *"If any of the following conditions are met, then skip these substeps for this element: ... The field element is not an input element whose type attribute is in the Image Button state, and either the field element does not have a name attribute specified, or its name attribute's value is the empty string."* - This explicitly excludes unnamed fields that are not input[type=image] from being submitted. – animuson Jul 04 '17 at 01:38
  • Using the W3C validator, an `input` tag with no `name` attribute is valid. Try it here: https://validator.w3.org/#validate_by_input – nmit026 Mar 14 '19 at 01:20
28

No.

I checked this in all browsers - the fields with empty/missing name are missing in POST/GET request from browser. It doesn't matter if they have or don't have id (my thought was that browsers might use id for name but no).

DenisS
  • 1,637
  • 19
  • 15
  • This particular experiment does not warrant that the data is "not submitted" from the browser. The other possible explanation for your output can be that the form did submit those values, but the server could not accept those values being unnamed. The specs do clarify the real case though. – Izhar Aazmi Sep 06 '18 at 08:26
0

it won't work directly but you can assign them through AJAX calls in JavaScript, idk really know if this really has any application in the real world ( one could be the obfuscation of the parameters that the server expects )

having

<form id="login" method="post" action="someurl">
 <input id="username" type="text" /> 
 <input id="password" type="password" />
 <input type="submit" value="login" />
</form>

JS to process would be (using jQuery to process ajax)

$("#login").on("submit",function(ev){
  $.post("someurl",{
    usrn: $("#username").val,
    pwd: $("#password").val       
  },function(ev){
          //this is the callback function that runs when the call is completed successfully
  });
}
/*second argument on $.post is and object with data to send in a post request 
usrn would be the name of the parameter recived in the server 
same for pwd "#username" and  "#password" are the id's html attribute for the field
'.val' is the jquery object's attribute in which jquery access the value in the text box
"$()" or it's equivalent "jQuery()" works like an object constructor that fills 
the attributes with the
DOM data that cover the css selector that this function expects as a parameter*/

please NOTE code might not be fully correct since i haven't test it but the logic behind it should be self-explanatory

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985