Ajax return with multiple result and creating lines

0

Due to an internal need, I need to make the return of an Ajax query create rows for each displayed result. Today I have a function that I use and that works normally when the result is only of one line, as below:

Now,ifthereturnisofmorethanoneresult,Igettheerrorbelow,insidetheJquerylibraryitself:

SoIsendJSON:

while(OCIFetch($consulta2)){$array=array('codigo'=>$v_codigo,'descricao'=>$v_descricao,'status'=>$v_status,'lote'=>$v_lote,'endereco'=>$v_endereco,'validade'=>$v_validade,'qtde'=>$v_qtde);echojson_encode($array);}

Thefunctionthat"gets" the JSON is this:

   // Atribui uma função para ser executada sempre que houver uma mudança de estado
    xmlreq.onreadystatechange = function () {
        // Verifica se foi concluído com sucesso e a conexão fechada (readyState=4)
        if (xmlreq.readyState == 4) {
          // Verifica se o arquivo foi encontrado com sucesso
            if (xmlreq.status == 200) {
                //Se o retorno foi vazio do Oracle
                if (xmlreq.responseText == "") {
                    document.getElementById("codigo").focus();
                   ids.forEach(function (id) {
                        document.getElementById(id).value = '';
                    });
                //Se encontrou dados
                } else {
                    //Aqui recebe os dados do processa.php, abre e aplica nos campos desejados
                    var dados = JSON.parse(xmlreq.responseText);
                    // função para preencher os campos com os dados
                    ids.forEach(function (id) {
                        document.getElementById(id).value = dados[id];
                    });
                }
            } else {
                result.innerHTML = "Erro: " + xmlreq.statusText;
            }
        }
        requestActive = false;
    };
    xmlreq.send(null);

It's the same function I'm trying to change to create table rows automatically when I find more than one result. I followed this example , I put id na table in html, but I could not change that ajax I use. Any suggestions?

UPDATE

With the answer from @Guerra, JSON switched to Ajax, and with the code below created the lines you needed:

   var HTML = "<table class='table table-striped table-bordered table-hover' style='width:500px'>";

                        HTML += "<tr><th>Status</th><th>Lote</th><th>Endereco</th><th>Validade</th><th>Qtde</th></tr>";

                        var data = JSON.parse(xmlreq.responseText);
                        document.getElementById("descricao").value = data[0].descricao; //POSIÇÃO 0 PARA SEMPRE PEGAR O PRIMEIRO NOME DO ARRAY
                        for(var i = 0;i<data.length;i++){
                          HTML += "<tr><td><input type = 'text' value=" + data[i].status + "></td>";
                          HTML += "<td><input type = 'text' value=" + data[i].lote + "></td>";
                          HTML += "<td><input type = 'text' value=" + data[i].endereco + "></td>";
                          HTML += "<td><input type = 'text' value=" + data[i].validade + "></td>";
                          HTML += "<td><input type = 'text' value=" + data[i].qtde + "></td></tr>";
                        }

                        HTML += "<tr><td colspan='7'><center><input type = 'button' value = 'Limpar' class='btn'";
                        HTML += "onclick='location.href='ConfirmaTransferencia.php''>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
                        HTML += "<input type = 'submit' value = 'Gravar' class='btn'></center></td></tr>";
                        HTML += "</table>";

                        document.getElementById('locais').innerHTML = HTML;
    
asked by anonymous 26.01.2017 / 14:11

1 answer

1

You are mounting the wrong JSON. You are not returning a Json with several lines, but several with a line.

The right thing would be to mount it this way:

while (OCIFetch($consulta2)){

     $array[] = array('codigo'=>$v_codigo, 'descricao'=>$v_descricao, 'status'=>$v_status, 'lote'=>$v_lote, 'endereco'=>$v_endereco, 'validade'=>$v_validade, 'qtde'=>$v_qtde);

}
     echo json_encode($array);

The returning Json should look something like this:

[
   {"codigo":"12312",...},
   {"codigo":"12312",...}
]
    
26.01.2017 / 16:54