Ajax return error - Json format with PHP

Asked

Viewed 51 times

2

Good evening. I am unable to find the error. I intend to build a list as I enter the date in the input date. Below is html:

    <div class="form-group">
                <label for="data_aula">Turma</label>
            <div >
              <select class="form-control" name="turma" id="turma">
                  <option></option>
                  <option value="Iniciante">Iniciante</option>
                  <option value="Avançado">Avançado</option>
                </select>
            </div>
             </div>

        <div class="form-group">
            <label for="data_aula">Informe a Data de Aula</label>
        <input  class="form-control" id="data_aula" name="data_aula" type="date" placeholder="dd/mm/aaaa" required="required" />
        </div>

Then below is the table that must be filled:

<form name="form1" class="form-horizontal" method="POST" action="processa/proc_notas_geral.php">
<div class="row">
    <div class="col-md-6">
        <table class="table table-striped table-hover table-condensed" id="tabela">
            <thead>
                <th>Foto</th>
                <th>Nome</th>
                <th>Marcar</th>
            </thead>
            <tbody>
                <!--aqui quero montar minha tabela -->
            </tbody>
        </table>

        <div class="form-group">
            <button type="submit" class="btn btn-success" id="sendListaAluno">Enviar Notas</button>
        </div>
    </div>
</div>

Now below the code to perform ajax:

<script type="text/javascript">

$(document).ready(function(){


    $('#data_aula').change(function() {

        var data_aula = $('#data_aula').val();

        var turma = $('#turma').val();


        if(turma.length == 0){
        alert("A turma não foi selecionada ainda.");
        return;
        }

        $.ajax({
                type    :   'post', 
                dataType: 'json',
                url     :   'processa/proc_lista_alunos_presentes.php',
                data    : {data_aula: data_aula, turma : turma},
                success: function (dados) {

                linha = "";
                if (dados == ""){
                    alert ("Não retornou nada!");
                    return;
                }
                for (i = 0; i < dados.length; i++) {
                    var caminho_foto = dados[i].imagem==""? "imagens/sem_imagem.png" : "fotos/" + dados[i].imagem;

                    linha += "<td hidden='true'>" + dados[i].id  + "</td>";
                    linha += "<td><img src=" + caminho_foto  + " width='60' height='60' name='foto_aluno' class='img-circle img-responsive'></td>";
                    linha += "<td style='text-align: left'>" + dados[i].nome + "</td>";
                    linha += "<td style='text-align: center'> <input type='checkbox' class='form-check-input'  value=" + dados[i].id  + "</td>";
                    $('#tabela tbody').html(linha);
                }
        }
      });
    })
 })

And in php contains the return as follows:

  <?php
session_start();
include_once("../seguranca.php");
include_once("../conexao.php");

$data_aula = $_POST['data_aula'];
$turma = $_POST['turma'];

$query_somente_alunos_presentes="SELECT 
 a.id_membro as id,
 a.nome, a.imagem
 FROM 
 MEMBROS a, FREQUENCIA c
 where A.id_tipo_membro=2  AND
 C.id_membro = A.id_membro AND
 C.data_aula = '$data_aula' AND
 A.turma ='$turma'
 ORDER BY nome;";

$sql = mysqli_query($conn,$query_somente_alunos_presentes);
$linhas=mysqli_num_rows($sql); 
if( $linhas > 0 ) {

     while($resultado = mysqli_fetch_assoc($qryLista)){
        $vetor[] = array_map('utf8_encode', $resultado); 
    }    
    //Passando vetor em forma de json
    echo json_encode($vetor);

    } 
?>

I do not know if there are any errors in the ajax parameters, or in my php return. It does not return anything, and when I go to the browser console does not point errors!

I inserted a.log console as the main one guided here, and the output came out as follows:

    (2) [{…}, {…}]
0
:
{id: "42", nome: "Antonio cesar paulo", imagem: "Antonio_1521556093.jpg"}
1
:
{id: "35", nome: "Artur Souza Oliveira", imagem: ""}
length
:
2
__proto__
:
Array(0)

I opened the Chrome developer tool went to the console. My table was not filled.

  • I inserted in my question the output I got in return ajax.

  • This $qryList variable that is in php code, picked it from somewhere else.. was it... Thank you very much!!!

  • @Paulogalego formulate an answer with what you have done or changed.

  • OK, do it now!

1 answer

0

was with error in the code that performs the query in the database, the variable '$qryLista', this out of context, the correct is: '$result'.

Browser other questions tagged

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