使用ajax和laravel在blade中显示搜索结果
我有一个客户搜索的地方,搜索产品将显示在刀片。我正在使用ajax和laravel。这是我写的代码。查询运行良好,当我打印$filter时,我可以看到结果使用ajax和laravel在blade中显示搜索结果,laravel,Laravel,我有一个客户搜索的地方,搜索产品将显示在刀片。我正在使用ajax和laravel。这是我写的代码。查询运行良好,当我打印$filter时,我可以看到结果 $('.gotosearch').on('click' , function() { var search = $(this).val() var inpvalue = $('.form-control').val() $.ajax({ url:'/searchproducts', ty
$('.gotosearch').on('click' , function() {
var search = $(this).val()
var inpvalue = $('.form-control').val()
$.ajax({
url:'/searchproducts',
type:'post',
data: {
inpvalue,
"_token" : token
},
success:function(r) {
console.log(r)
}
})
})
Route::post('/searchproducts','ProductController@searchproducts');
功能搜索产品(请求$search){
$filter=ProductModel::where('Product_Name','LIKE',$search->inpvalue.%')->get();
}
您可以在控制器中使用response()->json
public function CustomerSearch($getSearch){
$search = $getSearch;
$members = DB::table('tblcustomers')
->where('accountno', 'like', "%$search%")
->select('id','branchID', 'fullname', 'savingstype', 'accountno')
->orderby('id','asc')
->get();
return $members;
}
根据Laravel文件
json方法将自动将内容类型头设置为
application/json,以及使用
json_编码PHP函数:
试试这个:
returnresponse()->json($filters->toArray())代码>
试试这个:
//搜索文本
<div class="form-group">
<input type="text" id="search" name="search" class="form-control" id="exampleInputEmail" aria-describedby="emailHelp" placeholder="Search" value="{{ old('accountNo') }}" style="font-size:20px;font-weight:bold;" required>
</div
//路线
Route::get('/search/{searchQuerys?}', 'ContributionController@CustomerSearch');
像这样$filter=ProductModel::where('Product_Name','LIKE',$search->inpvalue.%')->get();return response()->json(['Product_Name'=>$filter]);非常感谢。我怎样才能显示它?在ajax中工作吗?如何在bladePost中显示呈现产品的代码
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#search').keyup(function(){
var search = $('#search').val();
if(search==""){
$('#getResult').css('display', 'none').hide();
}
else{
var getQueries = "";
$.get("{{ url('/') }}" + '/search/' + search,
{search:search},
function(data){
if(data){
$('#getResult').css('display', 'block');
}else{
$('#getResult').css('display', 'none')
}
if(search == ''){
$('#getResult').css('display', 'none').hide();
}else{
$.each(data, function (index, value){
var id=value.ID;
getQueries += '<ul style="margin-top:3px; list-style-type:none;">';
getQueries += '<a href={{ url("add-contribution-amount")}}' +'/'+ value.id + '>' + value.fullname +' | '+value.accountno + '</a>';
getQueries += '</ul>';
});
$('#getList').html(getQueries );
}
})
}
});
</script>
public function CustomerSearch($getSearch){
$search = $getSearch;
$members = DB::table('tblcustomers')
->where('accountno', 'like', "%$search%")
->select('id','branchID', 'fullname', 'savingstype', 'accountno')
->orderby('id','asc')
->get();
return $members;
}
Route::get('/search/{searchQuerys?}', 'ContributionController@CustomerSearch');