Reputation: 35
Is there something wrong with this code?
Form input:
<input type="text" id="currentTag" name="currentTag" class="inputbox"/>
jquery:
$("#currentTag").autocomplete({
source:'getautocomplete.php',
minLength:1
});
getautocomplete.php
$term=$_GET["term"];
$query=mysql_query("SELECT * FROM table_name WHERE tag_value LIKE '%".$term."%' ORDER BY tag_value ");
$results=array();
while($row = mysql_fetch_array($query)){
$results[] = array('label' => $row['tag_value']);
}
echo json_encode($results);
getautocomplete.php output when script is called directly:
[{"label":"birdseye"},{"label":"doggy"},{"label":"tomhat"}]
'SOLVED' It's a bit of a hack job, but I ended up setting source as a jquery var instead of url. Then used php include to echo the json into the var. All this in a Joomla site. Some conflict that I don't understand was happening, because the above code worked in a test file outside of Joomla. If anyone knows the conflict I'd curious to learn. Cheers.
$(document).ready(function() {
$( "#currentTag" ).autocomplete({
source: tags
});
});
var tags = <?php include("getautocomplete.php");?>;
Upvotes: 0
Views: 2181
Reputation: 2927
see this link http://jqueryui.com/autocomplete/
In the above link you will find demo source code see once
try this once
$( "#currentTag" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "getautocomplete.php",
dataType: "jsonp",
data: {
q: request.term
},
success: function( data ) {
response( data );
}
});
},
minLength: 3
});
Upvotes: 1
Reputation: 2489
There is a few thing wrong here, it could be any of the problems
$term
, because you give the availability of SQL injectionSELECT *
if you only need tag_value
Upvotes: 0