Interpret data from an external file to use in Autocomplete

0

I have the following file:

index.php

     

<script type="text/javascript">

function montaAutocomplete(source) {


 $( function() {



    var availableTags = [ source ];

    $( "#autocomplete" ).autocomplete({
      source: availableTags
    });

  } );

}

</script><script>
function busca_palavra(x){

  $.ajax({
    type:"GET",
    url: "autocomplete_sugestoes_descricao.php?q=" + x,
    dataType:'text',
    success : function(data) {

        montaAutocomplete( data );

      }
    });

}
</script>

And the file that generates the result:

autocomplete_sugestoes_descricao.php

<?php

require "../conexao_ajax.php";


$q                 = preg_replace("/[^0-9A-Za-z]-/", "",$_GET['q']);

$q_formatado       =  '%'.$q.'%';


if(!empty($q)){ 
    $busca_descricao = $link->prepare("SELECT id, descricao FROM ctrl_bancario WHERE descricao LIKE ? ORDER BY descricao ASC LIMIT 10");

    $busca_descricao->bind_param("s", $q_formatado);

    $busca_descricao->bind_result($id, $descricao);

    $busca_descricao->execute();

    $busca_descricao->store_result();

    if($busca_descricao->num_rows() > 0){

        echo "[";


        while ($busca_descricao->fetch()) {

            echo "$descricao, "; 

        }// Esta chave fecha o while ($busca_descricao->fetch()) { 

            echo "]";

       } // Esta chave fecha o if($busca_descricao->num_rows() > 0){


} // Esta chave fecha if(!empty($q)){   

?>

I am able to receive the data normally, however, if I have two records in my table:

Myapplicationisreturningthedatainthiswayinindex.phpautocomplete:

Asiftherewasnolinebreakorcorrectinterpretationofthedata.

HowcouldIproceedtohandlethisdatafromthefileautocomplete_sugestoes_descricao.phpinthevariablevaravailableTags=[source];?

Ihopetogetthisresult:

    
asked by anonymous 31.08.2017 / 15:22

3 answers

1

Your problem is in the form of the data:

[Depósito de Teste , teste, ]

Must contain quotation marks or apostrophes.

var teste = ["Depósito de Teste" , "teste", ];

Use json_encode for your php to return * in the correct format

echo json_encode($result);

  $( function() {
   
      var dados= ["Depósito de Teste" , "teste" ];

     $( "#teste" ).autocomplete({
      source: dados
    });
  } );
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script><scriptsrc="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  
  <div class="ui-widget">
  <label for="teste">teste </label>
  <input id="teste">
</div>
    
31.08.2017 / 15:50
1

Compile all the data coming from the database into an array and then return the array to your ajax using json_encode .

if ($busca_descricao->num_rows() > 0){
   $result = array();

    while ($busca_descricao->fetch()) {
        $result[] = $descricao;
    }
}

echo json_encode($result);

This will return an array formatted in

success: function(data) {
}
    
31.08.2017 / 15:53
0

I was able to find a solution:

var dados = [ source ];

var availableTags = JSON.parse(dados);

It worked perfectly, returning me this result:

Would it be safe to use this way?

    
31.08.2017 / 16:40