3
I’m trying to print a table using php, ajax and mysql.
I have this excerpt in Html
<!DOCTYPE html>
<html>
<head>
<title>Teste Ajax</title>
<?php
require("cabecalho.php");
?>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="ajax.js"></script>
</head>
<body>
<center>
<table border="1" width="500">
<thead>
<tr>
<th>Origem</th>
<th>Papel</th>
<th>Usuario</th>
</tr>
</thead>
<tbody id="listateste">
</tbody>
</table>
</center>
</body>
</html>
ajax.js
$(document).ready(function(){
$('#listateste').empty(); //Limpando a tabela
$.ajax({
type:'post', //Definimos o método HTTP usado
dataType: 'json', //Definimos o tipo de retorno
url: 'puxa.php',//Definindo o arquivo onde serão buscados os dados
success: function(dados){
for(var i=0;dados.length>i;i++){
//Adicionando registros retornados na tabela
$('#listateste').append('<tr><td>'+dados[i].origem+'</td><td>'+dados[i].Papel+'</td><td>'+dados[i].usuario+'</td></tr>');
}
}
});
});
AND PHP
<?php
header('Content-type: application/json');
include ("Conectcma.php");
$qryLista = mysql_query("SELECT * FROM reg_qm_papel", $conexao);
$resultado = array();
$resultado[] = mysql_fetch_assoc($qryLista);
echo json_encode($resultado);
?>
If I return the array inside PHP itself, the array is printed but apparently the value does not arrive in Js, I took mysql_fetch_assoc() from the loop just to test, but it did not make a difference Can anyone tell me what I’m doing wrong?
I put and gave error in the execution But I do not know if the problem is in this part, because php runs perfectly and returns me the array, but ajax does not
– Geraldão de Rívia
Here it worked normal, edit your question with the result of your ajax in dev tools, then I can help better.
– arllondias
I edited there, did not return anything :(
– Geraldão de Rívia
How did you manage to see that PHP was returning the database result correctly?
– arllondias
I ran the pull.php with a print_r inside and returned the array to me
– Geraldão de Rívia
Don’t you have another pull.php file in your project in a different folder? it may be that it is going to another page, because there really does not make sense..
– arllondias
Let’s go continue this discussion in chat.
– arllondias