Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a Javascript Array via JQuery Post so that all its contents are accessible via the PHP $_POST array?

How can I pass a Javascript Array via JQuery Post so that all its contents are accessible via the PHP $_POST array?

Please show an example of code that would do the trick.

Thanks!

like image 342
PleaseHelpMe Avatar asked Apr 06 '11 19:04

PleaseHelpMe


1 Answers

If you want to pass a JavaScript object/hash (ie. an associative array in PHP) then you would do:

$.post('/url/to/page', {'key1': 'value', 'key2': 'value'}); 

If you wanna pass an actual array (ie. an indexed array in PHP) then you can do:

$.post('/url/to/page', {'someKeyName': ['value','value']}); 

If you want to pass a JavaScript array then you can do:

$.post('/url/to/page', {'someKeyName': variableName}); 
like image 88
prodigitalson Avatar answered Sep 21 '22 21:09

prodigitalson