cpardon
cpardon

Reputation: 485

Ajax Append Multiple Value Array to formData

Project

Overall: I've built large form that includes a section for building dynamic sequences to demonstrate how to do some activity. This form currently submits all input data via ajax formData.

Focus: I'm trying to append a dynamic array for sequences, each to include an image ID & <textarea> description to an Ajax formData.

Problem

Not sure how to write my array to get both indexed items combined in the same array to send to server to create a comma-separated list for each sequence:

'ajax_seq_image[0]':'1',
'ajax_seq_desc[0]':"This is the value from textarea description for sequence[0]",
'ajax_seq_image[1]':'22',
'ajax_seq_desc[1]':"This is the value from textarea description for sequence[1]",
...

I have searced and tested a number of diffent approaches I've discovered, but none of these are getting me the result I'm looking for.

Current JSFiddle

I've built a jsfiddle for basic testing. So far I've been able to get the alerts (notice: they are commented out) to successfully show me the variables for each index [i], however I'm not able to append the new items to formData.

jsFiddle

Additional Info

To give idea of the input data that has been sent successfully & working correctly already...

// General Data
'ajax_unit_id'  : $('input[name =   unit_id]').val(),
'ajax_title'    : $('input[name =   title]').val(),
'ajax_status'   : $('select[name    =   status]').val(),
'ajax_access'   : $('select[name    =   access]').val(),
...

Upvotes: 0

Views: 1654

Answers (1)

Gagan
Gagan

Reputation: 130

var hiddenFields = '<form>';
$.each(object.data, function(k, v){
  hiddenFields +="<input type='hidden' name='"+k+"' value='"+v+"'>";
});

hiddenFields += '</form>';
var paramObj = $(hiddenFields).serialize();

Upvotes: 1

Related Questions