0
I’m creating a PHP query in a Mysql database. So far so good. The query works fine. I’m calling the query from a javascript using $.post('pagina.php',meusparametros,function(resultado){})
to insert the search result into my html page. I can even see the result appear in html when I do the test. The problem is that in the sequence it seems that the page gives a "refresh"... then add everything. =(
However, if I "print" the while for example in an Alert that is, it shows me the search result...
js file.
$(function(){
$("#btnPesquisa").click(function(){
var usr = $("#idUsr").val();
var dti = $("#dtinicio").val();
var dtf = $("#dtfinal").val();
if(usr != ""){
var sDados = {
usuario : usr,
dtinicio : dti,
dtfinal : dtf
}
$.post('pesquisa.php',sDados,function(retorno){
$(".resultado").html(retorno);
});
}else{
$(".resultado").html('');
}
});
});
php search.
require "conexao.php";
require "Usuario.class.php";
try{
$usr = $_POST['usuario'];
$dti = $_POST['dtinicio'];
$dtf = $_POST['dtfinal'];
$u = new Usuario();
$sDados = $u->pesquisaAcessos($usr,$dti,$dtf);
while($result=$sDados->fetch()){
echo '<li>'.$result['ocorrencia'].'</li>';
}
}catch(Exception $e){
echo '<script text="text/javascript"> alert("'.$e->getmessage().'")</script>';
}
html page.
<article class="cam-article">
<form class="menu-form" name="pesquisa" id="pesquisa" action="" method="POST">
<div class="menu-card">
<div class="menu-div-bar card-group">
<input type="text" id="idUsr" name="usuario" placeholder="Digite o nome do usuário">
</div>
<div class="menu-div-bar card-group">
<input type="date" id="dtinicio" name="dtinicio">
</div>
<div class="menu-div-bar card-group">
<input type="date" id="dtfinal" name="dtfinal">
</div>
<div class="menu-div-bar card-group">
<button type="submit" class="menu-btn" name="btnPesquisa" id="btnPesquisa">PESQUISAR</button>
</div>
</div>
</form>
<article class="cam-article">
<ul class="resultado">
</ul>
</article>
</article>
Show btnPesquisa code, it is probably sending the form.
– Rogerio Santos
Excuse me, I edited the question there, I do not know if this is how you do, I am new here in the forum... Tks!
– Rafael Dutra