Does form data still transfer if the input tag has no name?

泪湿孤枕 提交于 2019-12-27 09:04:18

问题


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.


回答1:


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




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/12543848/does-form-data-still-transfer-if-the-input-tag-has-no-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!