How to get php database values to javascript array -
hi here using jquery auto-complete plugin. have php retrieved database values want use values in autocomplete plugin. want php values javascript array. how can this?
$(function() { var availabletags = [ "actionscript", "applescript", "asp", "basic", "c", "c++", "clojure", "cobol", "coldfusion", "erlang", "fortran", ]; $( "#category" ).autocomplete({ source: availabletags }); });
php:
<?php require_once "config.php"; $q = strtolower($_get["q"]); if (!$q) return; $sql = "select distinct(category) completer"; $rsd = mysql_query($sql); while($rs = mysql_fetch_array($rsd)) { $fname = $rs['category']; echo "$fname" } ?>
js script :
$(function() { $.ajax({ type : 'get', url : 'urlofmyfile.php', data : 'q='+q, datatype : 'json', success : function(availabletags){ $( "#category" ).autocomplete({ source: availabletags }); } }); });
$.ajax documentation : http://api.jquery.com/jquery.ajax/
php script :
<?php require_once "config.php"; $q = strtolower($_get["q"]); if (!$q) return; $sql = "select distinct(category) completer"; $rsd = mysql_query($sql); while($rs = mysql_fetch_array($rsd)) { $fname[] = $rs['category']; } print json_encode($fname); exit; ?>
json_encode documentation : http://php.net/manual/en/function.json-encode.php
Comments
Post a Comment