I fairly new to JQuery and perhaps trying to achieve something that might be abit harder for a beginner. However I am trying to create an autocomplete that sends the curren
Finally found the solution that fits my needs
$("#login_name").autocomplete({
source: function(request, response){
$.post("/ajax/login_name.php", {data:request.term}, function(data){
response($.map(data, function(item) {
return {
label: item.user_login_name,
value: item.user_id
}
}))
}, "json");
},
minLength: 2,
dataType: "json",
cache: false,
focus: function(event, ui) {
return false;
},
select: function(event, ui) {
this.value = ui.item.label;
/* Do something with user_id */
return false;
}
});
A JSON strcuture is a flat string, while map expects an array or array like structure. try json decode on the string before using map.
Simple Jquery ui autocomplete, for those who might need it.
//select data from the table
$search = $db->query('SELECT Title from torrents');
//then echo script tags and variables with php
<?php echo '<script type="text/javascript">
$(function() {
var availableTags = [';
foreach ($search as $k) {
echo '"'.$k['Title'].'",';
}
echo '];
$( "#tags" ).autocomplete({
minLength:2, //fires after typing two characters
source: availableTags
});
});
</script>';
?>
your html form
<div id="search">
<form id="search-form">
<input id="tags" type="text" />
<input type="submit" value="Search" />
</form>
</div>
some suggestions:
dataType= "jsop"
? It doesn't appear to be jsonp. I think you want "json". cache : false
in the options, as well. This insures the request is always made, and never satisfied from browser-side cache.alert()
there. Does it get invoked? alert()
to verify the value of the parameters. Yes you do need header info for your json
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");
and tvanfosson makes a good point abut the the plug
in anycase I don't think you make the ajax call the plugin does.
if you are infact using jquery-ui autocomple you should read over the documentation get a basic version running. your php is fine aside from the missing header data
I had a problem like you too. And now I fix it. The problem is my json that return from my server contain a syntax error.
In http://api.jquery.com/jQuery.getJSON/ tells that if there are some error in JSON, it will fail silently. The JSON must match the JSON standard here http://json.org/ .
For my error is my string in JSON is wrapping in only one quote. But the JSON standard accept only string that wrap in double quotes.
eg. "Hello World" not 'Hello World'
When you fix it you can set the source as string URL. The term will be in "term" query string. And it works!!