0
Friends, good night. In my code, I send 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;
}
On the console, the data is sent as follows: (medicine ID - amount of output)
Content-Disposition: form-data; name="medicamentos"
2 - 2,1 - 3
What I’m trying to do is take the ID of the drug, and the amount of it dispensed, and register it in the dispensing table. I tried to make a foreach, but it returns to me in one 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 this foreach would have to be done, so that it appears 2x on the console.?
tried to do the
$medicamentos = json_decode($_POST['medicamentos']);
afterwardforeach ($medicamentos as $item => $value) echo $value->{'item_id'}
– adventistaam
No, I’m gonna try!
– Guilherme Lirio
@adventistaam Tested here, yet shows nothing in Alert! =/
– Guilherme Lirio
What you want to show on Alert?
– adventistaam
Because this script is just to show the text
– adventistaam
So what I’m trying to do is: when the person clicks on the register, separate the ID from the drug, and the amount, and add in the BD, the ID and the QNT. I’m still not adding because I’m testing hehehe!
– Guilherme Lirio
these data of the medicine are in table or where?
– adventistaam
They will be in a table afterwards, but are currently in 2 selects. They are sent via ajax. The ID - QNT is sent. Example: ["1" - "2"]
– Guilherme Lirio
Try it this way
foreach($medicamentos as $val){
 $medic = explode("-", $val);
 $id = $medic[0];
 $qtde = $medic[1];
 echo "ID: ".$id;
 echo "Qtde: ".$qtde;
 }
– adventistaam
@adventistaam managed to resolve already. I was with an error in my PHP. The return is that way even you said. Thank you very much!!
– Guilherme Lirio
What way? kkk said so much ... To add the answer... But I’m glad you solved
– adventistaam
It was the first way, helped me find the answer. But I missed sending JSON.stringify in Jquery.
– Guilherme Lirio
Cool. Very good!
– adventistaam