Well, I'm just trying to list the names I have in the bank, but the message undefined
, can anyone help? Below is the jquery + query to the bank.
jQuery:
<body>
<script>
$(document).ready(function() {
$('#relatorio').click(function(event) {
$.ajax({
type: 'post',
datatype: 'json',
url: 'ajax.php',
success: function(retorno){
for (var i = 0; i < retorno.length; i++) {
console.log(retorno[i].nome);
};
}
})
});
});
</script>
<input type="submit" name="Relatorio" value="Relatorio" id="relatorio">
</body>
Consulted with the bank:
<?php
$conect = conecta();
$select = seleciona($conect);
echo json_encode($select);
function conecta() {
$server = "localhost";
$usuario = "root";
$senha = "";
$database = "teste";
$conexao = mysqli_connect($server, $usuario, $senha) or die();
mysqli_select_db($conexao, $database);
return $conexao;
}
function seleciona($conect) {
$guarda = array();
$query = "SELECT * FROM teste";
$executa = mysqli_query($conect, $query);
$i = 0;
while ($resultado = mysqli_fetch_array($executa)) {
$guarda[] = array('nome'=> (utf8_encode($resultado['nome'])));
}
return $guarda;
}
?>