0

I have to append some information in my JS with form data. Something like..

var form_data = new FormData();
form_data.append('photo', photo_data );
form_data.append('customer_id', customer_id);
form_data.append('year_name', year_name);

As we can see I have used .append thrice for three information. I tried something like this and did not work as .append() expects two parameters(field name and value) and not object.

var data = {'photo': photo_data, 'customer_id': customer_id, 'year_name', year_name};
form_data.append(data);

I wish to know if there is some way to pass multiple field names and values in a single line. Thanks!

6
  • In first line you are using formdata and then you start using form_data? Commented May 4, 2016 at 6:13
  • Sure, if all your fields are in the same <form>, you can directly call new FormData(yourForm) Commented May 4, 2016 at 6:19
  • Add Array values to FormData is described here .. How to add an array value to new FormData? Commented May 4, 2016 at 6:21
  • Ps: if all you got is this object, you can also simply iterate over your object's keys : Object.keys(data).forEach(function(k){form_data.append(k, data[k]);}) Commented May 4, 2016 at 6:25
  • Sorry, a typo @Mike . Edited the question. Commented May 4, 2016 at 6:52

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.