views:

65

answers:

1

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

+3  A: 

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().

jAndy
OK jAndy think I get it, but the cols are not in a form so is this what you mean?var senderobj = [];$('#id^=col').each(function(){ senderobj.push($(this).serialize());}$.ajax({ url: "test.php", type: "post", data: senderobj, error: function(){ alert(serial_1); } });
I updated the example. Sortable's method 'serialize' will convert the id's into a form/ajax submittable string internally.
jAndy
Thanks again jAndy, please accept my thanks, and my stupidity, I'm very used to php but JQuery/Java is something I avoided for years. So I really am "stupid" here. now are you saying that I could do everything in one "fowl swoop" like this:var senderobj = [];$('.column[id^=col]').each(function(){ senderobj.push($(this).sortable('serialize');}if(window.JSON) senderobj = window.JSON.stringify(senderobj);$.ajax({//...data: { $.ajax({ url: "test.php", type: "post", data: senderobj,}});column is the css class for each col (col1,col2 etc.) If I recall id^ increments the id oc col?
@russp: you're very welcome. id^=somestring just tells the selector (sizzle) to only grab elements which id begins with 'somestring'.http://api.jquery.com is a worth a read to learn more.
jAndy