I am reading my Karaoke list from a DB and that works well but want i want to do is be able to type in a string in a search form and as I type it starts to load songs and/or artists that match.
I know the basics of what I need but not sure on what I need to do the auto-complete?
any help or resource will will be Helpful
Here is the jQuery UI Autocomplete documentation:
http://jqueryui.com/autocomplete/
Here is an example of how it should be implemented:
var AutoCompleteOptions = { source: function( request, response ) {
$.ajax({
url: 'your URL here',
dataType: "json",
data: {
itemToSearch: request.term // could be any data you are passing in for search
},
success: function(data) {
// do something where search values are returned
},
});
},
select: $.proxy(function(event, ui){
// what you want to do with that information
// using a proxy to preserve the reference to 'this'
return false; // prevent the default response (typically inserting the selected value into the textbox the dropdown is being displayed from.
},this),
open: function(event, ui) {
// things to do when the dropdown is rendered
return false; // prevent default autocomplete open action
},
focus: function(event, ui) {
// what to do when an the user hovers over an item in the drop down
return false; // prevent default autocomplete open action
},
minLength:0 // be sure to set this if you want to be able to trigger the search event manually and have it display results
};
var Input = $("input");
Input.autocomplete(this.AutoCompleteOptions)
You can use jQuery autocomplete, Include library and depended files as in the order it should appear here is autocomplete
PHP code
public function cities() {
$term = $_GET['term'];
$cities = array("one","two","three");// contains value fetched from DB
$filtered = array();
foreach ($cities as $city) {
if (stripos($city, $term) !== false) {
array_push($filtered, $city);
}
}
echo json_encode($filtered);
exit;
}
jQuery code
<script>
$(function() {
function log( message ) {
$( "<div>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}
$( "#textboxid" ).autocomplete({
source: "cities",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
</script>