Passing a serialised form and 2D array via ajax

Viewed 48

I'm creating a system for work, and need to pass both HTML form contents and a two dimensional array to PHP via ajax. I'm not sure if I'm going along the right lines with the below, or if this is possible:

var myArray = [][];

    $.ajax({
        type : 'POST',
        url : 'url',
        data : $('#form').serialize() + myArray.serialize();
    }

Many thanks in advance for any advice!

1 Answers

You should add name for both the data values which helps you to get the data sent on the another page.And serialize() won't work for encoding array values into url format what you need it join() function here.

var myArray =  [1,2,3,4,5];

    $.ajax({
        type : 'POST',
        url : 'url', //Enter the correct url.
        data : {
                data1:$('#form').serialize() + myArray.join('&')
               }

Output

FirstName=Mickey&LastName=Mouse1&2&3&4&5 //Just an example

And then get the data in the php as

$data1 = $_POST['data1'];
Related