it looks like your getresults() expects a q parameter to be passed, so in data, send q with the value of the search box
data: { q: $('#search-box').val() }
Jul 5, 2017
13
Level 1
Autocomplete with AJAX -data issue
Hi, Im trying to add autocomplete option to a search product field, This is the function
public function getresults () {
$q = Request::get ( 'q' );
$Sproduct = Product::where ( 'title', 'LIKE', '%' . $q . '%' )->get();
if (count ( $Sproduct ) > 0)
return view ( 'content.search' )->withDetails ( $Sproduct )->withQuery ( $q );
else
return view ( 'content.search' )->withMessage ( 'No Details found. Try to search again !' );
}
}
this is the view
<form action="http://localhost/myshop/public/search" method="POST" role="search">
{{ csrf_field() }}
<div class="input-group">
<input type="text" class="form-control" name="q" id="search-box"
placeholder="Search products"> <span class="input-group-btn">
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</form>
I don't knwo how to take the data (The data is the titels from products table)
so the script is, I don't know what to put in Data,
$.ajax({
$(document).ready(function(){
$('#search-box').on('keyup', function () {
var userText = $(this).val().trim();
if (userText.length > 0) {
$.ajax({
url: BASE_URL + 'shop/search',
type: "GET",
dataType: "json",
data: ??
success: function (response) {
if (response) {
var availableTags = [];
$.each(response, function (key, value) {
availableTags.push(value.title);
});
$('#searchField').autocomplete({
source: availableTags,
select: function (event, ui) {
$('.submit-btn').click();
}
});
Please help me, thanks.
Please or to participate in this conversation.