0
Hello
I am trying to insert an array, but in this case, you can insert multiple lines in db.
How do I pass the data to codeIgniter:
var productsData = [];
$(".productRow").each(function(i){
var pData = {
id_product: $(this).find(".tdProductId").attr("id"),
amount: $(this).find(".tdProductAmount").text(),
price: $(this).find(".tdProductPrice").text()
};
productsData.push(pData);
});
$.ajax({
url: "<?php echo site_url('/StockController/createInputStock')?>",
type: "POST",
data: {
id_people: $("#people option").attr("id"),
type: $("#stock_type").val(),
date: $("input[name=date]").val(),
products: JSON.stringify(productsData)
},
success: function(data){
Materialize.toast(data, 4000);
},
error: function(data){
console.log(data);
Materialize.toast('Erro ao adicionar uma nova entrada de estoque!', 4000);
}
});
});
Controller:
public function createInputStock(){
$people = array(
'input_date' => $this->input->post('date'),
'input_type' => $this->input->post('type'),
'id_people' => $this->input->post('id_people')
);
if($this->StockModel->createInputStockPeople('stock_input',$people)){
$product = array(
'id_product' => $this->input->post('id_people'),
'unit_price' => $this->input->post('id_people'),
'amount' => $this->input->post('id_people'),
'id_stock' => '51'
);
foreach ($product as $products) {
if($this->StockModel->createInputStockProduct('stock_input_products', $products)){
echo "foi";
}else{
echo "deu pau";
}
}
}else{
echo "Erro ao adicionar a pessoa";
}
}
Models:
public function createInputStockPeople($table, $peopleData){
$this->db->insert($table, $peopleData);
return $this->db->insert_id();
}
public function createInputStockProduct($table, $produtctData){
return $this->db->insert($table, $produtctData);
}
As you can see, only one row is inserted in the table stock_input
(and this already works) the problem is to then insert several in the table stock_input_products
Well, the method you are doing is already passing the data to codeigniter, including inserting it into the database. What is going wrong?
– LF Ziron
simply did not insert in stock_input_products, that product array is right? It returns me the msg of ajax error
– zyzzete
Can add error message to question?
– LF Ziron