javascript - jQuery AJAX with Multiple Array data Parameters -
i've posted single array, can't figure out how send more 1 array in ajax post. here code 1 array:
var = new array(); // fill array var a_post = {}; a_post['array1[]'] = a; $.ajax({ url: "submitorder.php", data: a_post, type: 'post', success: function(data) { alert(data); } }); and in submitorder.php have:
$array1= $_post['array1']; foreach ($array1 $a => $b) echo "$array1[$a] <br />"; this works fine. however, when try add second array b_post data: field, doesn't work. tried data: {a_post, b_post}, , few variations of that, can't work properly. while i'm @ it, how load submitorder.php after posting rather show alert of data?
update
using nicolas' suggestion, got work changing data field to:
data: {'array1':json.stringify(a), 'array2':json.stringify(b)}, however, need add rest of form data has been input user. can data $(this).serialize() if try add data field, not work. how can add data above line?
thanks.
solution
what ended working way had hoped (with nicolas' help):
var formdata = $(this).serializearray(); var a_string = json.stringify(a); formdata.push({name: 'array1', value: a_string}); var b_string = json.stringify(b); formdata.push({name: 'array2', value: b_string}); $.ajax({ url: "submitorder.php", data: formdata, type: 'post', success: function(data) { alert(data); } });
the data should encapsuled way
data: {'first_array':json.stringify(array1),'second_array':json.stringify(array2)} then in php:
$array1 = json_decode($_post['first_array']); $array2 = json_decode($_post['second_array']); you can add rest of inputs well.
data: {'first_array':json.stringify(array1),'second_array':json.stringify(array2),'input1':$(input[name="input1"]).val()} just repeat inputs want send.
'input1':$(input[name="input1"]).val(),'input2':$(input[name="input2"]).val(),... etc
Comments
Post a Comment