Return multiple elements by ajax

Asked

Viewed 60 times

0

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

<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);

1 answer

2

Instead of returning a json, you can ride on php the HTML structure that will be inserted in the page, inside the while.

But first, I suggest you change the tags td for th, thus they will be interpreted as titles of each column.

In the archive PHP called by ajax, you can do so:

$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;

Browser other questions tagged

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