I need to get a count of items in my Mysql DB using ajax and php.
My ajax looks like this:
<code>
$(document).ready(function(){
$('#badgewel').empty(); //Limpa a tabela
$.ajax({
type:'get', //método HTTP usado
dataType: 'json', //tipo de retorno
url: '../badge.php',//arquivo php onde serão buscados os dados
success: function(dados){
$('#badgewel').text(dados);
}
}
});
});
</code>
And my PHP file looks like this:
<code>
<?php
$con = mysqli_connect('XXXX','XXXXXX','XXXXX','XXXXXX');
if (!$con){
die('Não pode conectar:' .mysql_error($con));
}
mysqli_select_db($con,"badge");
$sql="SELECT
idProd,
COUNT(idProd) AS Total
FROM Produtos";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);
echo $row ['Total'];//testei a pagina badge.php e funcionou
echo json_encode($row);
mysqli_close($con);
?>
</code>
In HTML I have a section where the data will be displayed (a bootstrap badge):
<code>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>teste badge</title>
<link rel="stylesheet" href="css/style.css">
<link rel="author" href="humans.txt">
<script src="../js/badgewel.js" type="text/javascript" charset="utf-8" async defer></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script></head><body><div><h3>Issoéumtestedebadge</h3><span><label>Total </label><pid="badgewel" class="badge"></p>
</span>
</div>
</body>
</html>
</code>
It happens that it is not displaying anything, it had to display the total but nothing happens!