PHP does not display array data!

Asked

Viewed 68 times

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.?

  • 1

    tried to do the $medicamentos = json_decode($_POST['medicamentos']); afterward foreach ($medicamentos as $item => $value) echo $value->{'item_id'}

  • No, I’m gonna try!

  • @adventistaam Tested here, yet shows nothing in Alert! =/

  • What you want to show on Alert?

  • Because this script is just to show the text

  • 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!

  • these data of the medicine are in table or where?

  • 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"]

  • Try it this way foreach($medicamentos as $val){&#xA; $medic = explode("-", $val);&#xA; $id = $medic[0];&#xA; $qtde = $medic[1];&#xA; echo "ID: ".$id;&#xA; echo "Qtde: ".$qtde;&#xA; }

  • @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!!

  • What way? kkk said so much ... To add the answer... But I’m glad you solved

  • It was the first way, helped me find the answer. But I missed sending JSON.stringify in Jquery.

  • Cool. Very good!

Show 8 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.