Have recently found a solution to this problem:

$.post() accepts a JavaScript object for post data e.g. {name1 :
value1, name2 : value2}, but you need to specify the same object
property multiple times.
Therefore you can create this property once, put an array into it and
then push your values into that array.

assuming vars 'url', 'postData', 'name' and 'value' already declared:

// is php array syntax being used? i.e. ends with []
if(name.charAt(name.length -2) == '[' &&  name.charAt(name.length -1)
== ']') {
    // create array if needed
    if(postData[name] === undefined) {
        postData[name] = [];
    }
    // push value onto array
    postData[name].push(value);
}
// not using php array syntax so add in normal way
else postData[name] = value;

// post to server
$.post(url, postData, function(data) {
        // handle response
});

Hope this helps.

On Feb 11, 6:43 am, "Konstantin Mirin" <konstantin.mi...@gmail.com>
wrote:
> I need to pass array to $.ajax() function and get this array on the server
> side.
> If doing this manually, I should get: script.php?arr[]=val1&arr[]=val2
> $.ajax() only encodes like script.php?arr=val1&arr=val2 and I get only the
> last value on the server side... any ideas how to do it without custom
> function?
>
> Best regards,
> Konstantin Mirin
>
> mailto:konstantin.mi...@gmail.com
> mailto:i...@konstantin.takeforce.net

Reply via email to