Problems with no screen refresh queries

Asked

Viewed 34 times

-1

Friends, I am trying to do a database search and present the result on the screen without updating the page, however when typing in the input no message is displayed.

The user would start typing a name and the names similar to the ones typed should appear on the screen.

I’m using a code I saw in a tutorial and so I’m not really sure what it’s doing, even though it’s a very simple code.

This is the excerpt from my HTML:

<div class="pess_painel" id="pess_ajusta_painel">
    <h1 class="display-2">Ficha do Aluno</h1>
    <h4 >Você pode buscar um aluno cadastrado ou cadastrar um novo para abrir a página dele</h4>
        <div class="pess_centralizar pess_tipo_ajuste">
            <div class="pess_item_div">
                <input type="text" name="getAluno" class="form-control pess_centralizar" id="getAluno" placeholder="Buscar Aluno" required>
            </div>
            <p>ou</p>
            <div class="pess_item_div">
                <button class="btn btn-info btn-block"  data-toggle="modal" data-target="#addAlunoModal">Cadastrar Novo Aluno</button>
            </div>
        </div>
    <ul class="aluno_resultado">
      
    </ul>
</div>

These are the scripts used:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.11/jquery.mask.min.js"></script>
<script src="https://cdn.rawgit.com/PascaleBeier/bootstrap-validate/v2.2.0/dist/bootstrap-validate.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

This is my function, which should update the screen with the results of the query:

$(function(){
  $("#getAluno").keyup(function(){

    var pesquisa = $(this).val();
    if (pesquisa != '') {
      var dados = {
        palavra : pesquisa
      }
      $.post('busca_alunos.php', dados, function(retorna){
              
        $(".aluno_resultado").html(retorna);
      });
    }else{
      $(".aluno_resultado").html('');

    }
  });
});

And following the file busca_alunos.php, in it for now, I do not display the return of the query, I am displaying only a standard message saying whether or not found the registration.

<?php
require_once 'verifica.php';
require_once 'conexao2.php';

    $alunos = $_POST['palavra'];

    global $pdo;

    $sql = "SELECT * FROM ALUNOS where NOME LIKE :nome  ";
    $sql = $pdo->prepare($sql);
    $sql->bindValue(':nome', "%".$alunos."%", PDO::PARAM_STR);
    
    $sql->execute();


    if($sql->rowCount() <= 0){
        echo "Nenhum Aluno Encontrado.";
    }else{
        echo "Aluno Encontrado";
    }  
?>
  • Guys, there is something wrong with my question, if you have please let me know so that I can reformulate it and try to improve it, because you have 'denied' my question but did not inform the reason, then I do not know how to improve and ask for someone actually to help me with doubt.

  • Good night friend, returns some error?

  • Good morning Eduardo, no error returns, that is why it is difficult to identify what is actually happening, but in my tests we understand that the problem is here $.post('busca_alunos.php', dados, function(retorna) because I placed some test messages inside the.php.php query and none of the messages were displayed in my tests.

1 answer

0


The problem occurred because of a conflict generated with this import <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

After removing this script my code worked perfectly.

Browser other questions tagged

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