How to generate query and add new input?

Asked

Viewed 56 times

0

I have a field input of the kind date I want every time I click and view it generates a select with the date query I have set and generate a new input to add another date and so on.

My code so far:

HTML FORMULARIO

<form class="form-inline" role="form" style="margin-left: 10px;" action="#">
 <div class="form-group">
 <label for="relatorio">Visualizar Relatorio: </label>
 <input type="date" class="form-control" id="relatorio" name="relatorio">
 </div>
 <div class="btn btn-success m-l-10" onclick="consulta()">Adicionar</div>
 <div class="btn btn-success m-l-10" onclick="">Remover</div>
</form>

HTML RESULTS

<table>
        <thead>
          <tr>
            <th>Cod.</th>
            <th>Descricao</th>
            <th>Status</th>
            <th>Valor</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        foreach ($dia as $b) {?>
        <tr>
            <td><?php echo $b->id; ?></td>
            <td><?php echo $b->descricao; ?></td>
            <td><?php echo $b->status; ?></td>
            <td><?php echo $b->valor; ?></td>
        </tr>
        <?php } ?>
    </tbody>

MYSQL:

"SELECT *, SUM(valor) as valo FROM pedidos WHERE data LIKE :data GROUP BY `data` ORDER BY id DESC"

My biggest problem is that I don’t know how to generate the foreach and querys why every time I add a new date with the reports will generate a new foreach.

How to do?

1 answer

1


With PHP it will not be possible to do this "dynamically" you will have to launch the data through an AJAX request and each time this request returns some value, ie return the HTML, you insert it with document.querySelector(".classe");.

Edit
When the user clicks(configure) the date, it will launch the function connect(), this function could be something like:

function connect()
{
    var relatorio = document.getElementById("relatorio").value;
    $.post("query.php", {data: relatorio}, function(response){
        // response é os dados do HTML lá do PHP
        document.getElementById("resultado").innerHTML += response;
    });
}
query.php
//VOCE PRECISA FAZER O FOREACH NO PHP
//Esse script vai retornar HTML do FOREACH
header("Content-Type", "text/html");

$data = $_POST['data']; //recebendo de $.post (javascript)

$query = "SELECT * FROM table WHERE {$data}"; //faça sua query, porém insira a variável $data

//mande para o banco de dados

//FAÇA O FOREACH AQUI DO HTML E ARMAZENE EM UMA VARIAVEL

//echo $VARIAVEL_COM_O_HTML DO FOREACH

I don’t know what your end result should be, I’m not a seer. But you are aware of what you need to do there. Until.

  • And how would I do the querys and foreach? About AJAX I know I would have to enter but I don’t know how to generate the queys and foreach.

  • See the update @Evertonfigueiredo

  • Now I think I can only have a doubt rsrs how do I make the variable not repeat?

  • Guy when I add the new input that submit the new input the previous one with the data will not be lost?

  • ...innerHTML += response; the += here takes what was before and adds up what will be inserted.

Browser other questions tagged

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