1
Hello I am trying to make a query in a table and show the results on the client page without the need to update the page every new registration. But I’m not getting it. Follow the codes:
index php.
<button id="fetchLojas" class="btn btn-primary" type="button" data-toggle="collapse" data-target="#selloja" aria-expanded="false" aria-controls="collapseExample">
Consultar/Excluir/Editar lojas
</button>
<div class="collapse" id="selloja">
<div class="well">
<h1 class="page-header">Alteração de loja <small>Escolha qual loja você quer altera ou excluir</small></h1>
<h1 class="page-header"><small>Todas lojas</small></h1>
<div class="" id="mostraLojas">
</div>
<button class="btn btn-danger" type="button" data-toggle="collapse" data-target="#selloja" aria-expanded="false" aria-controls="collapseExample">
Voltar
</button>
</div>
</div>
fetch js.
$(document).ready(function() {
$("#fetchLojas").on("click", function() {
$.ajax({
url: "../php/fetchLojas.php"
}).done(function(data){
$(this).empty().append("<li>"+ data +"</li>");
});
});
});
fetchLojas.php
<?php
require_once "dbconf.php";
$dbconf = new db_connection;
$dbconf->connect_db(); //estabelecendo conexão com o banco de dados
$conn = $dbconf->conn;
$sql = "SELECT * FROM lojas ORDER BY id_lojas DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title"><b>Loja '. $row["nome"].'</b>
  <a href="http://digyx.com.br/bkp/lojas/editar-lojas.php?loja='.$row['id_lojas'].'" data-toggle="tooltip" data-placement="top" title="Editar"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
<span class="sr-only">Editar Loja</span>
  <a href="http://digyx.com.br/bkp/lojas/exclui-lojas.php?loja='.$row['id_lojas'].'" data-toggle="tooltip" data-placement="top" title="Exluir"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a>
<span class="sr-only">Excluir Loja</span>
</h1>
</div>
<div class="panel-footer">
<h4><b>Nome Banco: </b>'. $row["nomebanco"].'</h4>
</div>
<div class="panel-footer">
<h4><b>Nome arquivo .sql: </b>'. $row["nomearquivo_sql"].'</h4>
</div>
<div class="panel-footer">
<h4><b>Nome arquivo .tar.gz: </b>'. $row["nomearquivo_targz"].'</h4>
</div>
<div class="panel-footer">
<h4><b>Usuário do banco de dados: </b>'. $row["bduser"].'</h4>
</div>
<div class="panel-footer">
<h4><b>Diretorio da loja: </b>'. $row["pasta_loja"].'</h4>
</div>
</div>
<hr>
';
}
} else {
echo "Nenhuma loja cadastrada!";
}
$dbconf->close_db(); //fechando conexão com banco de dados
What I want is when I click on the Browse Stores button (id="fetchLojas"), the query happens and shows the data in the div (id="showLojas"), but when I click on the button shows nothing.
If anyone can help me see what’s wrong, I’d appreciate it!!!
Hint, return the structure to a json or xml, and populate html using js
– MarceloBoni
I don’t know how to do it =(
– Jorge Borgonhi Batista
Something like this: http://stackoverflow.com/questions/3351882/convert-mysqli-result-to-json
– MarceloBoni
while($row = $result->fetch_array(MYSQL_ASSOC)) {
 $myArray[] = $row;
 }
 echo json_encode($myArray);
– MarceloBoni
while, like this? $myArray[$i]="content and variables"; $i++; outside while json_encode($myArray);
– Jorge Borgonhi Batista
That, have to mount an array with all the content you want to convert into json and at the end give a
echo json_encode($myArray);
– MarceloBoni
By directly accessing your php script it displays something?
– Lucas Torres
Works by accessing direct, but takes the browser to the php page. I already found the answer elsewhere. Thanks!
– Jorge Borgonhi Batista