Error in listing filter with php and jquery

Asked

Viewed 41 times

0

I was trying to make a list filter, I would like every time that the select changes its value to update my table, but there’s an error that I can’t find, when starting the page or changing the select no value is returned in my table and is displayed to error Alert.

Index.html

<select id="filtro">
  <option> Alunos </option>
  <option> Professores </option>
 </select> 

 <br> <br>

 <table border="1"> 
    <tr align="center">
       <td> Nome </td>
       <td> Email </td>
       <td> Telefone </td>
       <td> RG </td>
       <td> CPF </td>
    </tr>
    <tr>
       <td id="nome"> </td>
       <td id="email"> </td>
       <td id="tel"> </td>
       <td id="rg"> </td>
       <td id="cpf"> </td>
    </tr>
 </table>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script>
  $(document).ready(() => {

     filtro();

     $('#filtro').change(() => {
        filtro();   
     });   

  });

  function filtro(){

    var data = {
       acao: "filtro",
       filtro: $('#filtro').val(),
    };

    console.log(data);

     $.ajax({
        type: "POST",
        url: "listagem.php",
        dataType: "json",
        data: data,
        sucess: (data) => {

           data = JSON.parse(data);

           $('#nome').html(data.nome);
           $('#email').html(data.email);
           $('#tel').html(data.tel);
           $('#rg').html(data.rg);
           $('#cpf').html(data.cpf);

        }, error: () => {
           alert('Ocorreu algum erro.');    
        }    
     });

  }
</script>

php listing.

include('conexao.php');

$data = $_POST ? $_POST : $_GET;

$response = [
  'error' => ''
];

if(!$data){
   $response['error'] = 'Nada na requisição';
}

if($data['acao'] == 'filtro'){

   $query = "SELECT * FROM login WHERE nivel={$data['filtro']}";
   $result = mysqli_query($con,$query);

   while($row = mysqli_fetch_assoc($result)){
      $response = $row;    
   }

}

return json_encode($response);
  • "is displayed to error Alert." right, and already tried to see what the error, inspect the message to try to understand the problem? use }, error: (request, status, error) => { and see what’s in the variables that ajax returns

  • the error returned is this: Syntaxerror: Unexpected token < in JSON at position 0, you know tell me what might be?

1 answer

0

The mistake Syntaxerror: Unexpected token < in JSON at position 0 occurs because it is trying to parse for JSON something that is already in JSON format due to dataType: "json" set in the object of the request and the use of json_encode() on the backend side. Something that might be causing problems is also your file php listing., once you are using return in return json_encode($response); outside the scope of a function (function). Change in the ajax request object the word sucess for success, remove data = JSON.parse(data); and try so on the PHP side:

<?php

include('conexao.php');

$data = $_POST ? $_POST : $_GET;

$response = [
  'error' => ''
];

if(!$data){
  $response['error'] = 'Nada na requisição';
}

if($data['acao'] == 'filtro' && isset($data['filtro'])){

  $query = "SELECT * FROM `login` WHERE `nivel` = '{$data['filtro']}'";

  $result = mysqli_query($con,$query);

  while($row = mysqli_fetch_assoc($result)){
     $response = $row;    
  }

}

echo json_encode($response);

I hope I’ve helped.

Browser other questions tagged

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