How can I send an array to php through ajax?

3 Answers

You might do that with $.post method of jQuery (for example) :

var myJavascriptArray = new Array('jj', 'kk', 'oo');

$.post('urltocallinajax', {'myphpvariable[]': myJavascriptArray }, function(data){
   // do something with received data!

Php will receive an array which will be name myphpvariable and it will contain the myJavascriptArray values.

Is it that ?


I want to send an array constructed in javascript with the selected values of a multiple select. Is there a way to send this array to a php script using ajax?

$php_array = json_decode($_POST['cars']);

How to send multiple ajax requests?

Use the for loop to generate an array of items (push the different itemids into the array on each iteration) and then make only one AJAX call in which you'll insert all of the items to the database. This way will also optimize your site and save your bandwidth.

Write to Session with Post - CakePHP

With Cake, to get posted values in $this->request->data, their names have to be prefixed with data:


$.post('/LocalClocks/editSelected', { "data[Session][selected]" : selected }, function(data){


function editSelected()
      $this->Session->write('selected', $this->request->data['Session']['selected']);