0

After setting a new data-order_item to my divs, I'm trying to post an array to my controller with the data_order and the data_id:

  var counter = 0;
  item_cnt = new Array(); 
  cnt.find('.item_incompleted').each(function(){
    counter += 1;
    $(this).attr("data-order_item", counter);
    item_element = new Array();
    item_element['order'] = $(this).attr("data-order_item");
    item_element['item_id'] = $(this).attr("data-item_id");
    item_cnt.push(item_element);
  });
  url = cnt.attr('data-order_route');
  $.ajax({
    type: "POST",
    url: url,
    data: item_cnt,
  });

By console logging item_cnt the array is fine, however, by var_dumping $_POST in the controller the array is empty and it seems like it's not posting the array at all.

Dani
  • 84
  • 1
  • 14
  • Check the network tab in the developer console and see if anything is being sent – Musa Jan 02 '17 at 15:57
  • I did and nothing is being sent, I can't figure out why though. – Dani Jan 02 '17 at 15:59
  • You must convert the array to json, before you can sent it via ajax. Then on the PHP site, you need to convert the json back. [See here](http://stackoverflow.com/questions/2295496/convert-array-to-json) – KhorneHoly Jan 02 '17 at 15:59
  • 1
    1. `item_element` should be an `object` and not an `array` 2. `data` has to be an object with key-value-pairs or a correctly encoded querystring – Andreas Jan 02 '17 at 16:00
  • It works creating an object, thanks! – Dani Jan 02 '17 at 16:39

0 Answers0