I've script like this
$(function(){
//attach autocomplete
$("#user_key").autocomplete({
//define callback to format results
source: function(req, add){
//pass request to server
$.getJSON("/ajax/user_autocomplete.php?user_key=?",req, function(data){
//create array for response objects
var suggestions = [];
//process response
$.each(data, function(i, val){
suggestions.push(val.name);
});
//pass array to callback
add(suggestions);
});
}
});
});
and want to send one param to php file for receiving answer , how can I do it within this script ? and is it neccessary to have in html for method="get" or doen't it matter ?
I've to write {param: $(this).val} instead of req or how ?
The req
argument will be a object containing the attribute term
. This, and any other GET parameters, will need to be defined for the second argument for $.getJSON
function(req, add){
var params = {
user_key: myUserKeyVar,
term : req.term,
otherKey: myOtherKey
};
//pass request to server
$.getJSON("/ajax/user_autocomplete.php",params, function(data){
//create array for response objects
var suggestions = [];
//process response
$.each(data, function(i, val){
suggestions.push(val.name);
});
//pass array to callback
add(suggestions);
Just do:
$('#user_key').autocomplete(
{
source: '/ajax/user_autocomplete.php'
});
As long as you send back correct JSON from user_autocomplete.php then all is good.