Friends, good night. In my code, I submit the following:
function cadastrar(){
var t = 0;
var nome = $$("#nome").val();
var cpf = $$("#cpf").val();
var tel = $$("#telefone").val();
var end = $$("#endereco").val();
var medic = $$("#lista_medicamentos .item-inner");
var medicamentos = [];
medic.each(function(idx, li){
t++;
var id_med = $$(li).find(".item-id-"+t+"");
var nome_med = $$(li).find(".item-title-"+t+"");
var qnt_med = $$(li).find(".item-after-"+t+"");
medicamentos.push(id_med.html() +' - '+qnt_med.html()); //cria o array
})
var form_data = new FormData();
form_data.append('paciente', nome);
form_data.append('cpf', cpf);
form_data.append('telefone', tel);
form_data.append('endereco', end);
form_data.append('medicamentos', JSON.stringify(medicamentos));
$.ajax({
url: 'cadastrarP.php',
type: 'POST',
data: form_data,
cache: false,
contentType: false,
processData: false,
success: function(response) {
console.log(response);
},
error: function(error) {
alert(xhr.response);
}
});
return false;
}
In the console, the data is sent as follows: (Drug ID - output quantity)
Content-Disposition: form-data; name="medicamentos"
2 - 2,1 - 3
What I am trying to do is to get the ID of the medicine, and the amount of it dispensed, and register in the dispensing table. I tried to do a foreach, but it returns everything to me on a single line, without the comma: 2 - 21 - 3
<?php
header('Access-Control-Allow-Origin: *');
//header('Content-Type: application/json;charset=utf-8');
include 'init.php';
if(isset($_POST['paciente'])){
$paciente = $_POST['paciente'];
$cpf = $_POST['cpf'];
$telefone = $_POST['telefone'];
$endereco = $_POST['endereco'];
$medicamentos = json_decode($_POST['medicamentos']);
foreach($medicamentos as $val){
echo $val;
}
}
How would this foreach have to be done so that 2x appears in console.log?