Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an array value to new FormData?

On form submission I'm using jQuery to gather data including files and creating a FormData Object of the form values using:

var formData = new FormData($("form#formid")[0]);

but how can I add another value and it's key to this FormData Object?

like image 474
Dan Avatar asked Jun 26 '12 13:06

Dan


2 Answers

You can also use FormData.set().

The difference between FormData.set and append() is that if the specified key already exists, FormData.set will overwrite all existing values with the new one, whereas append() will append the new value onto the end of the existing set of values.

Syntax:

formData.set(name, value);
like image 55
Lucky Avatar answered Sep 30 '22 16:09

Lucky


var data = new FormData(),
    fields = $("#myForm").serializeArray();

$.each( fields, function( i, field ) {
    data.append(field.name, field.value);
});
like image 44
Alex Avatar answered Sep 30 '22 17:09

Alex