Print table with ajax and php

Asked

Viewed 1,275 times

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?

By dev tools tb does not return me anything inserir a descrição da imagem aqui

1 answer

2


In my case, I’m with a version where the error that the mysql function is deprecated, just add this line at the beginning of your file pulls.php:

ini_set('display_errors', 0);

Although it’s not advisable. My Answer at devtools, yours has to be something like this, check:

inserir a descrição da imagem aqui

  • 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

  • Here it worked normal, edit your question with the result of your ajax in dev tools, then I can help better.

  • I edited there, did not return anything :(

  • How did you manage to see that PHP was returning the database result correctly?

  • I ran the pull.php with a print_r inside and returned the array to me

  • 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..

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.