user351657
user351657

Reputation: 361

JQuery make an array - how/what is best

I have 4 serailized arrays that I want to pass to php for processing. What is the best way to combine them into a single array>

example:

serial_1 = $('#col1').sortable('serialize');
serial_2 = $('#col2').sortable('serialize');
serial_3 = $('#col3').sortable('serialize');
serial_4 = $('#col4').sortable('serialize');`

each serialized array relates to a column/section of the page (col1,col2 etc.) What I need to do/would like to do is create a single array that puts the serialized array inside another array for a single post.

example:

var new_array = serilaize(col_1(serial_1),col2(serial_2),col3,(serial_3),col4(serial_4))

I KNOW THAT IS NOT RIGHT as I have no idea in JQuery how to right the correct syntax.

This new array is to be posted via ajax like this:

        $.ajax({
    url: "test.php",
    type: "post",
    data: new_array,
    error: function(){
    alert('SOME ERROR MESSAGE');
    }
    });

Thanks in advance

Upvotes: 0

Views: 308

Answers (1)

jAndy
jAndy

Reputation: 236122

You may just create an overlaying object/array and jsonize it, like

var senderobj = [];
$('*[id^=col]').each(function(){
  senderobj.push($(this).sortable('serialize');
}

if(window.JSON) senderobj = window.JSON.stringify(senderobj);

$.ajax({
//...
data: {
   formdata: senderobj
}
});

replace the universal selector (*) with the element type you need

To create an array you can always call $.makeArray() which creates a true javascript array from an 'array-like object'. Infact, calling .serialize() does not much more than calling .serializeArray() internally which in turn, calls .makeArray().

Upvotes: 3

Related Questions