Send AJAX to a table

Asked

Viewed 29 times

0

I have in a database data page inserted in a table:

<div class="procurar">
    <input type="text" id="search" onkeypress="mandar()"><i class="fa fa-search" aria-hidden="true"></i>
</div>

<div class="historico">

<table class="table" id="dados_entradas">

<?
    $historico = mysql_query("SELECT ut.nome, ut.contribuinte, en.id_ficha, DATE_FORMAT(en.data_entrada,'%d-%m-%Y') as data
                              FROM entrada en,utilizador ut
                              WHERE en.contribuinte = ut.contribuinte");

    while($linhas = mysql_fetch_array($historico))
    {
?>
    <tr>
        <td style="width:25%;"><?echo $linhas['nome']; ?></td>
        <td style="width:25%;"><?echo $linhas['contribuinte']; ?> </td>
        <td style="width:25%;"><?echo $linhas['id_ficha']; ?> </td>
        <td style="width:25%;"><?echo $linhas['data']; ?></td>
    </tr>

<?  
    }
?>

</table>    

And then I did an input with onkeypress to send in AJAX a query, to send to this same table.

AJAX

    function mandar()
{
    var search=$("#search").val();

    $.post("dados-entradas.php",{search:search}, function(data){
        $("#dados_entradas").html(data);

});
}

And on the page dados-entradas.php

$search = $_POST['search'];

    $historico = mysql_query("SELECT ut.nome, ut.contribuinte, en.id_ficha, DATE_FORMAT(en.data_entrada,'%d-%m-%Y') as data
                              FROM entrada en,utilizador ut
                              WHERE en.contribuinte = ut.contribuinte
                              AND ut.nome = '$search'");

    while($linhas = mysql_fetch_array($historico))
    {

    echo"<table class='table' id='dados_entradas'>  
    <tr>
        <td style='width:25%;'>".$linhas['nome']."</td>
        <td style='width:25%;'>".$linhas['contribuinte']."</td>
        <td style='width:25%;'>".$linhas['id_ficha']."</td>
        <td style='width:25%;'>".$linhas['data']."</td>
    </tr>
    </table>";


    }

The problem is that it does not return any value, and defaces the entire home page except the table.

1 answer

2


Note the following, you declare the table as follows:

<table class="table" id="dados_entradas">

However, on the.php input page you declare again the line above, when you should only declare the tr’s.

That’s the first situation. I noticed that in your sql, you put to search for exact match:

ut.nome = '$search'

When could it be:

ut.nome LIKE '%" . $search . "%'

Browser other questions tagged

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