Autocomplete with data coming from PHP

2

I'm trying to create a form to register services performed, so I came across the first problem, I'm trying to keep it as light as possible because a lot of data. I want to store the "employee", "client", "services" ... I chose to use an autocomplete input bringing the name of the DB clients, however when it passes the 10 names it stops.

clientlist.php

<?php   
$query_clientes = 'SELECT * FROM clientes';
$result_clientes = mysqli_query($conectar, $query_clientes);
while ($linhas_clientes = mysqli_fetch_assoc($result_clientes)){  
echo '"'.htmlentities($linhas_clientes['nome']).'",';
}?>

HTML

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>autocomplete demo</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.12.4.js"></script>
  <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>

<label for="autocomplete">Clientes: </label>
<input id="autocomplete">

<script>
var tags = [ <?php include_once("listaclientes.php"); ?>];
$( "#autocomplete" ).autocomplete({
  source: function( request, response ) {
          var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
          response( $.grep( tags, function( item ){
              return matcher.test( item );
          }) );
      }
});
</script>

</body>
</html>

The generated code looks like this:

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>autocomplete demo</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
      <script src="//code.jquery.com/jquery-1.12.4.js"></script>
      <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    </head>
    <body>

    <label for="autocomplete">Clientes: </label>
    <input id="autocomplete">


    <script>
var tags = [ "Junior Santos","Michel","Cordeiro","Elton Costa","Mateus Eduardo","Eduardo","Deivinho","Rogerio Ribeiro","Sergio Ribeiro","Manuel Luiz Santos","Claudio Gomes","Evandson dos anjos        
","Edmilson Cabral      
","Junival alexandre        
","Brendo Luiz      
","Jessica Sabino   ","Jhonatas","Paulo Vitor","Rafael Casal","Jaison Felix","Rubens Arruda",];
$( "#autocomplete" ).autocomplete({
  source: function( request, response ) {
          var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
          response( $.grep( tags, function( item ){
              return matcher.test( item );
          }) );
      }
});
</script>

    </body>
    </html>
    
asked by anonymous 30.10.2018 / 07:13

1 answer

3

The problem is that line breaks are generating a Javascript error.

The solution is to clear these special characters in echo :

<?php   
   $query_clientes = 'SELECT nome FROM clientes';
   $result_clientes = mysqli_query($conectar, $query_clientes);
   while ($linhas_clientes = mysqli_fetch_assoc($result_clientes)) {

      $nome = htmlentities($linhas_clientes['nome']); // Tratamento de caracteres especiais
      $nome = str_replace(array("\r", "\n"), $nome);  // Remoção das quebras de linha

      echo '"$nome",';
   }

The str_replace changes the characters of the first parameter array("\r", "\n") by the second parameter '' , effectively removing them from string .

More details in the manual:

  

link

  

link


Tip:

instead of this

$query_clientes = 'SELECT * FROM clientes';

Prefer this

$query_clientes = 'SELECT nome FROM clientes';

First of all, even if you only use nome , it will traffic all fields from DB to PHP. By the second, you're just bringing the information you really need.

More details at:

  

Why is "SELECT * FROM table" bad?

    
30.10.2018 / 08:04