I have my autocomplete working fine if there is hard coded data being fed into it. My PHP is returning a JSON result. I’m not sure where I am going wrong.
HTML
<div class="form-group bgcolor">
<label for="send_to">Direct to: </label><input type="text" name="send_to" id="send_to" class="form-control send_to typeahead" placeholder="Leave blank normally">
</div>
Jquery
$('.typeahead').typeahead({
source: {
groupName: {
ajax({
url: 'scripts/order_messaging.php',
method: 'POST',
data: JSON.stringify({action: 'autocomplete_to_user', query:query}),
contentType: 'application/json',
dataType: 'json',
success:function(data)
{
result($.map(data, function(item){
return item;
}));
}
})
},
},
debug: true
});
PHP
//autocomplete user name for user_to
if ( $_POST['action'] == 'autocomplete_to_user' ) {
$stmt = $pdo->prepare('select * from login where username like :query');
$stmt->bindValue('query', '%'.$_POST['query'].'%');
$stmt->execute();
$result = array();
while($user_name = $stmt->fetch(PDO::FETCH_OBJ)) {
array_push($result, $user_name->username);
}
echo json_encode($result);
}
I think it’s this line in my jQuery: data: {action: 'autocomplete_to_user', query:query},
Maybe I have a syntax problem.
4
Answers
Well, after much trial and error I got an autocomplete working using jQuery .autocomplete.
I don't know what I was doing wrong with the typeahead but the documentation was hard to understand (probably because of my limited experience with jQuery).
To all those in the future that need some help with this; here is a tutorial I found that was helpful: jquery ui autocomplete tutorial
Thank you to everyone
As per jQuery Ajax Documentation,
dataType: 'json'
evaluates the response as JSON and returns a JavaScript object.You need to stringify your data by using
JSON.stringify({action: 'autocomplete_to_user', query:query})
before you send it to PHP. Also, you need to add headerContent-Type: 'application/json'
that tells you PHP code that request data is JSON. You can do this by addingcontentType: 'application/json'
in your Ajax settings.Your final jQuery code would look like this:
Refer to jQuery Ajax Documentation for mode details.
Hope this helps!
EDIT:
You need to update your PHP code to read JSON. Please refer to this link.
Your PHP Code should look like this:
I’ve tested the library and you need to change two things in order to make it work.
1) You need to restructure the
source
parameter. You were giving it an AJAX callback which is not supported. According to the documentation, it accepts an array or an object with settings, so AJAX needs to be defined like this:The
path
property of theajax
object is key here. It tells thetypeahead
function where to look for the data that was returned by the AJAX action. Since you’re encoding a one-dimensional array of values, you need to set it to an empty string, meaning your data is in the root of the response.If you were to return, for example, an array like
echo json_encode(['users' => $result]);
then you’d have to set the path to'users'
(because that is the index in the response that holds your data).2) You have to follow a strict HTML structure in order to make it work:
If you leave out any of these elements, it doesn’t trigger the functionality. This HTML structure is one of the first things they mention in the documentation.
In my humble opinion, the documentation has some errors. For instance, in the demos, specifically the example Country v2 states
when in actuality the request being sent in the example is
GET
, because it depends on thetype
key of theajax
object of the first source (country in this case), which is not set, thus defaulting toGET
.So, that being said, you really should stick to the proposed HTML structure (at least start with it, then take away stuff you don’t want gradually as long as it’ll let you).
HTML
JS
order_messaging.php
HIH