Return multiple elements through ajax

0

My question is as follows, I have a table in MySQL with id , pesquisa and data . I want to give select to this table in php and return ajax so that I can insert each element into a specific td within a table . How can I do this?

<div class="janelaLateral">
    <table id="tdTeste" name="tableConteudoPesquisado" cellpadding="0" cellspacing="0">
        <tr>
            <td>id</td>
            <td>Data</td>
            <td>Pesquisa Realizada</td>
        </tr>
    </table>
</div>


function fImprimir(){
            $.ajax({
                url: "php/listarPesquisaRealizadaBanco.php",
                type: "GET",
                dataType: "json",
                success:function(ret){

                    $("#tdTeste").append(ret);

                }
            });

        }

$resultado = mysqli_query($conn, "SELECT * FROM tabelaProva");

$contador = 0;

while($row = mysqli_fetch_assoc($resultado)){
    $retorno[$contador] = $row["id"];.$row["pesquisa"].$row["data"];
    $contador++;
}

mysqli_close($conn);

echo json_encode($retorno);
    
asked by anonymous 20.04.2018 / 16:48

1 answer

2

Instead of returning a json , you can set the php to the HTML structure that will be inserted into the page, within while .

But first, I suggest you change the tags td to th , so they will be interpreted as headings in each column.

In the file PHP called by ajax , you can do this:

$resultado = mysqli_query($conn, "SELECT * FROM tabelaProva");
$retorno = '';

while($row = mysqli_fetch_assoc($resultado)){ 
   $retorno .= '<tr>' .
                  '<td>' . $row["id"] . '</td>' .
                  '<td>' . $row["pesquisa"] . '</td>' .
                  '<td>' . $row["data"] . '</td>' .
               '</tr>';
}

mysqli_close($conn);

echo $retorno;
    
20.04.2018 / 17:23