Tomasz Tomasz - 15 days ago 8
MySQL Question

Who can help me with autocomplete

I have problem with jquery autocomplete with database its not working.

My code

test5.php:

<?php
$wynik = mysql_query("SELECT * FROM nabk_t_item_tags")
or die('B&#322;&#261;d zapytania');
if(mysql_num_rows($wynik) > 0) { while($r = mysql_fetch_assoc($wynik)) {
$input = $r['tags'] ;
$fields = explode(',', $input);
foreach($fields as $field) {
echo ''.$field.'<br>'; }}}
?>


and javascript

<script type="text/javascript">
$(function() {
var availableTags = <?php include('test5.php'); ?>;
$("#tags").autocomplete({
source: availableTags,
autoFocus:true
});
});
</script>


please help me what i doing wrong?

Answer

try this

<?php
     $wynik = mysql_query("SELECT * FROM nabk_t_item_tags") 
         or die('B&#322;&#261;d zapytania'); 
     if(mysql_num_rows($wynik) > 0) { while($r = mysql_fetch_assoc($wynik)) { 
       $input = $r['tags'] ;
       $fields = explode(',', $input);
       foreach($fields as $field) {
    echo '"'.$field.'",'; }}} 

?>

 <script type="text/javascript">
$(function() {
    var availableTags = [<?php include('test5.php'); ?>];
    $("#tags").autocomplete({
        source: availableTags,
        autoFocus:true
    });
});
</script>