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.php
inthevariablevaravailableTags=[source];
?
Ihopetogetthisresult: