1

I have a page with 3 forms on it.

To create a valid get(or post) data str with each form as a nested array nested arrays.

psuedo-code for what i want (just don't know how to wrap them in nested arrays..):

var data_str = $('#form1').serialize() + $('#form2').serialize() + $('#form3').serialize();
$.ajax(.....

desired output on processing file:

print_r($_GET);
/*
{
    ['form1'] => .....nested array here..
    ['form2'] => .....nested array here..
    ['form3'] => .....nested array here..
}
*/

3 Answers 3

1
$.get({
data:{
   form1:$('#form1').serialize(),
   form2:$('#form2').serialize(),
   form3:$('#form3').serialize()
},

//other options
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Praveen, but this just creates 3 GET/POST values and no nested arrays. There are serialized strings instead of nested arrays...
1

I'm not a javascript pro but I've built a solution and it works, enjoy:

function mutliple_forms_to_data_str(array_of_form_ids){
    var multidim_data = {};
    $.each(array_of_form_ids, function(index,id) {
        var forms_data = $('#'+id).serializeArray();            
        var htemp = {};
        $.each(forms_data, function(index,val) {
            htemp[val.name] = val.value;
        });
        multidim_data[id] = htemp;
    });    
    return multidim_data;
}

Comments

0

Use array push, to do that

but honestly you best use one form no need for all three of them unless is last resource. or simply.

var data_str = {
     'form1': $('#form1').serialize(),
     'form2': $('#form2').serialize(),
     'form3': $('#form3').serialize(),
    }

PHP would read them fine as a tree like array,

You might have to use serializeArray(); instead

4 Comments

php reads this like:` Array ( [form_1] => 'data_str1' [form_2] => 'data_str2' [form_3] => 'data_str3' )`
it's 3 forms because it's part of a webapp, and the forms can be submitted separately.
interesting getting close. serializeArray(), gives [0] => Array ( [name] => inputnamehere [value] => 2 ) [1] => Array ( [name] => inputnamehere [value] => 2011-03-06 ) So I'm losing my KEY => VALUE relationship
ok, got it working will post my solution shortly for other's reference.

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.