Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormData created from an existing form seems empty when I log it [duplicate]

I'm trying to get a set of keys (input names or similar) and values (input values) from a web form:

<body>   <form>     <input type="text" name="banana" value="swag">   </form>    <script>     var form = document.querySelector('form');     var formData = new FormData(form);   </script> </body> 

According to the FormData documentation, formData should contain the keys and values from the form. But console.log(formData) shows formData is empty.

How can I quickly get the data from the form using FormData?

JSFiddle

like image 239
mikemaccana Avatar asked Jul 30 '14 15:07

mikemaccana


People also ask

Why is my form data empty?

Solution. When you initialize FormData() from a form element, it'll only map input fields that have a name attribute. Only these mapped input fields are included in the form data that is sent in requests. Hence, when none of the input fields are mapped, the form data will be empty.

How do I check if a FormData is empty?

val(); if(name && pret){ $. ajax({ url: 'connect. php', type: 'POST', data: formData, async: false, cache: false, contentType: false, processData: false, success: function(){ alert('uploaded successuflly! '); } }); }else{alert('input fields cannot be left empty you num num!

Why FormData is empty in jquery?

FormData wil support multi file upload. Should work great! NOTE: You may find that the FILES array is empty on server side page - In this case you need to make sure your server configuration allows file uploads, size limit of file upload is enough, post time is enough etc....


1 Answers

Update: the XHR spec now includes several more functions to inspect FormData objects.

FireFox has supported the newer functions since v39.0, Chrome is slated to get support in v50. Not sure about other browsers.

var form = document.querySelector('form'); var formData = new FormData(form);  for (var [key, value] of formData.entries()) {    console.log(key, value); }  //or  console.log(...formData) 
like image 200
Farray Avatar answered Oct 10 '22 21:10

Farray