2
Within my system, the user logs in, some information searched in my database is shown, follows function that searches the data and how they are listed:
Function
# função que busca os dados de originação no banco
function listaDadosOriginacao($conexao){
$dados = array(); //pode ser [] também, mas por compatibilidade, array()
$rede = $_SESSION['redeSelecionada'];
$codLoja = $_SESSION['lojaSelecionada'];
$mes = $_SESSION['mesSelecionado'];
$resultado = mysqli_query($conexao, "SELECT * FROM evolucao_originacao WHERE codLoja = {$codLoja} AND rede = '{$rede}' AND mesReferencia = '{$mes}'");
while($valores = mysqli_fetch_assoc($resultado)){
array_push($dados, $valores);
}
return $dados;
};
Listing (a single example because all data belong to the same array)
<div class="col-lg-2 col-md-6 col-sm-6 col-xs-12">
<a class="dashboard-stat dashboard-stat-v2 green" href="#">
<div class="visual">
<i class="fa fa-shopping-cart"></i>
</div>
<div class="details">
<div class="number"><span data-counter="counterup" data-value="<?=number_format($dadosOriginacao['propostasAprovadas'],0,',','.')?>">0</span></div>
<div class="desc"># Aprovadas</div>
</div>
</a>
</div>
I have two dropdowns that filter data and when clicking a button, it goes to the database through an AJAX request and searches all the data in the same table that I use to enter information to the user and filters using the values selected in the dropdown.
Follow what I already have from the AJAX request:
$("#botao-filtrar").click(function(){
$(".mask-loading").fadeToggle(1000);
$.ajax({
url: 'datacenter/functions/filtraDashboardGeral.php',
async: true,
type: 'POST',
dataType: 'JSON',
data: {rede: $("#dropdown-parceria").val(), codLoja: $("#dropdown-loja").val(), mes: $("#dropdown-mes").val()},
success: function(data){
}
});
});
And this is the document quoted
filtraDashboardGeral.php
<?php
session_start();
require_once('../../includes/gestaoOriginacao.php');
$rede = $_POST['rede'];
$codLoja = $_POST['codLoja'];
$mes = $_POST['mes'];
$dados = array();
$resultado = mysqli_query($conexao, "SELECT * FROM evolucao_originacao WHERE redeTratada = '{$rede}' and codLoja = {$codLoja} and mesReferencia = '{$mes}'");
while($valores = mysqli_fetch_assoc($resultado)){
array_push($dados, $valores);
}
What I’m trying to do is, that ajax returns me the data and fill in the new values in the variables I’m already using for listing without I need to reload my page!
I know it’s a bit complex and I’ve been racking my brain about it for weeks and nothing works. I’m a beginner in this area of asynchronous requests.
Any solution or improvement suggestion?
thank you very much, I was able to receive the data, in Success I put a console.log and came an array with my data filtered the way I need, but I have no idea how to do the inclusion part of the data in my html with jquery. Could you help me? (I already gave a moral by clicking on the arrow)
– João Vitor
Thanks for the moral! : ) I’m leaving now, but I’ll leave you some guidance on what you’ll need: A) Locate the elements in the page’s DOM using $("#minhaid") , $(". minhaclasse") or similar mechanisms and then B) use a change of the element value or internal text of the element or add elements dynamically. Have a look at: http://www.w3schools.com/jquery/jquery_dom_add.asp and https://learn.jquery.com/using-jquery-core/manipulating-elements/
– Antonio Alexandre
But I place the call of the DOM element along with append, after, etc inside the Success?
– João Vitor
It can be right there in sucess or call another function to do this. One thing I like to do that helps me is to send html directly already format because I think assemble everything with more laborious javascript. I’ll paste an example into another reply using html upload instead of json.
– Antonio Alexandre