69

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.

3 Answers 3

83

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

6
  • 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. Sep 22, 2012 at 17:55
  • 1
    Can someone quote the precise HTML5? Jul 6, 2014 at 16:52
  • 2
    @CiroSantilli六四事件法轮功纳米比亚威视 w3.org/TR/html5/… Oct 7, 2015 at 9: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 :-) Oct 7, 2015 at 9:43
  • 5
    @CiroSantilli Very late response, but the specific information you want is actually under Form Submission, 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 4, 2017 at 1:38
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).

1
  • 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. Sep 6, 2018 at 8: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.