MoteCL
MoteCL

Reputation: 228

try to create an array in js then send to controller

Im try to send an array to my controller in Codeigniter,To then be able to use this array in my controller, but gettting a problem in the creation the array repeat the line enter image description here

here my js function

 function get_array(){
   var datos = [];
   row = {};
  $("#tbl_esctructura tbody > tr").each(function() {
    var item = $(this).find('td:eq(1)').text();
    var cantidad = $(this).find('td:eq(3)').text();
     row["item"] = item;
     row["cantidad"] = cantidad;
     datos.push(row); // you push it to the array
  });

    datos =  JSON.stringify(datos);
}

here my controller

public function data_from_array(){
    $data   =  array($this->input->post('datos', TRUE));

    foreach ($data as $row) {

        echo $row;
    }
}

Upvotes: 0

Views: 29

Answers (1)

Sorav Garg
Sorav Garg

Reputation: 1067

Please modify your code like that.

 function get_array(){
   var datos = [];
  $("#tbl_esctructura tbody > tr").each(function() {
    var row = {};
    var item = $(this).find('td:eq(1)').text();
    var cantidad = $(this).find('td:eq(3)').text();
     row.item = item;
     row.cantidad = cantidad;
     datos.push(row); // you push it to the array
  });

    datos =  JSON.stringify(datos);
}

Upvotes: 1

Related Questions