I have a table like in this image and I want to get the data from it and save it with this ajax function, but it seems it isn't passing data to php. When I return $this->request->data on my php code the console.log() is trigered at the .fail showing several functions, and when I return json_encode($this->request->data) the .done is triggered but console.log() shows an empty array.
Edit: console.log(JSON.stringify(array)) =
[{"SIA_ID":"1070","DESCONTO_CNES":"100,00","VALOR_PAGO":"679,19"}]
What am I doing wrong?
It seems that I can't post images, so here is a link: https://i.stack.imgur.com/Ov0G5.jpg
Sorry about my English, hope you can understand.
$("#modal-btn-salvar-desconto").on("click", function() {
var dadosTabela = $("#tabela-estabelecimento").bootstrapTable("getData");
var array=[];
$(dadosTabela).each(function(index) {
array[index] = {
"SIA_ID" : this.ID,
"DESCONTO_CNES" : $("#modal-valor-desconto").val().replace("R$", ""),
"VALOR_PAGO" : this["VALOR_A_PAGAR"]
};
});
console.log(JSON.stringify(array));
$.ajax({
url: "ajaxAtualizar",
type: "POST",
dataType: "json",
data: JSON.stringify(array)
})
.done(function(data) {
var data = JSON.parse(data);
if (!data.salvou){
new PNotify({
icon: false,
text: '<h5>Registro não pôde ser gravado!<h5>',
type: 'error'
});
return;
}
$(".modal-estabelecimento").modal("toggle");
$("#mensagem-ajax").html("<div class='" + (data.salvou ? "alert alert-secondary fade in" : "alert alert-warning fade in") + "'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>X</button>" + data.mensagem + "</div>");
})
.fail( function(data) {
console.log("FALHOU");
console.log(data);
return;
});
});
public function ajaxAtualizar() {
$this->layout = "ajax";
$this->autoRender = false;
if ($this->request->is("ajax")) {
foreach ($this->request->data as $dados) {
$dados["desconto_cnes"] = str_replace(",", ".", str_replace(".", "", $dados["desconto_cnes"]));
$dados["desconto_rubrica"] = str_replace(",", ".", str_replace(".", "", $dados["desconto_rubrica"]));
$dados["valor_pago"] = str_replace(",", ".", str_replace(".", "", $dados["valor_pago"]));
$existeId = $this->Pagamento->findBySiaId($dados["sia_id"]);
if ($existeId) {
$dados["id"] = $existeId["PagamentoAmbulatoriai"]["id"];
}
}
if ($this->Pagamento->saveAll($dados)) {
return json_encode(array("salvou" => true, "mensagem" => $this->Mensagem->Msgi(7)));
} else {
return json_encode(array("salvou" => false, "mensagem" => $this->Mensagem->Msgi(6)));
}
}
}