0
I have 3 files:
novaDose.php:
<label for="busca">Buscar cidadão:</label>
<input type="text" class="form-control" id="busca" placeholder="Digite parte do nome ou o CNS" onkeypress="busca(this.value);">
<br/>
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">CNS</th>
<th scope="col">Nome Completo</th>
<th scope="col">Data de Nasc</th>
</tr>
</thead>
<tbody id="tabelaPaciente">
<?php
include 'conexao.php';
$result = mysqli_query($con, "select * from paciente limit 20");
while ($item = mysqli_fetch_assoc($result)){
$dataNasc = date_create($item["dataNasc"]);
echo '<tr>
<td>'.$item["cns"].'</td>
<td>'.$item["nomePaciente"].'</td>
<td>'.date_format($dataNasc, "d/m/Y").'</td>
</tr>';
}
?>
</tbody>
</table>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="novaDose.js"></script>
searchPHP:
include 'conexao.php';
$result = mysqli_query($con, "select * from paciente");
echo json_encode($result);
mysqli_close($con);
novaDose.js:
function busca(){
$('#tabelaPaciente').empty(); //ATÉ AQUI FUNCIONA
$.ajax({
type:'post',
dataType:'json',
url:'buscar.php',
success: function(dados){
alert(dados); //ESSE ALERTA NÃO FUNCIONA
for(var i=0;dados.length>i;i++){
//Adicionando registros retornados na tabela
$('#tabela').append('<tr><td>'+dados[i].cns+'</td><td>'+dados[i].nomePaAciente+'</td><td>'+dados[i].dataNasc+'</td></tr>');
}
}
});
};
Apparently everything works ok, but I can not succeed in the requisition. Use firefox and in the console and network debug modules are not returning any error.
if it doesn’t work it’s because it doesn’t work from Success, invert the echo line with mysql close.
– Julio Henrique
@Juliohenrique same thing
– Italo Rodrigo
puts this on top of echo . header('Content-type: application/json');
– Julio Henrique
@Juliohenrique still, with no return. see that I have an Alert(data) in the Success field but it does not run. how can I show any error?
– Italo Rodrigo