The response should show no results as I clear the input fields for the search feature but instead it is giving the full collection. It works fine when there is input but once I clear it the whole collection is showing. Here is the Laravel Controller Class.
public function search(Request $request)
{ if($request->ajax()){
$results = DB::table('PROPERTY_TAGS')->where('TAG_NAME','LIKE','%'.$request->search."%")->get();
$output='';
if(count($results)>0){
foreach($results as $result){
$output.='<p>'.$result->TAG_NAME.'</p>';
}
}else{
$output="<p>no data found</p>'";
}
return $output;
}
}
Here is my HTML with laravel blade template:
<div class="row">
<div class="col s9 "> <input type="text" id="search" name="search" placeholder="Search" /></div>
<div class="col s3"><a href="sass.html"><i class="material-icons">search</i></a></div>
<div class="col s12">
<b>Ex: </b><i>Beltola, Zoo Road</i>
<!-- Suggestions will be displayed in below div. -->
<div id="display"></div>
</div>
</div>
Here is the script:
<script type="text/javascript">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#search').on('keyup',function(){
$value=$(this).val();
$.ajax({
type : 'GET',
url : '/search',
data:{'search':$value},
success:function(data){
if(data == '\0'){
$('#display').text('');
}
$('#display').html(data).show();
//alert(data.success);
}
});
});
</script>
You don't need to make the Xhr request if the string is empty. Could add a little check for that as shown below.
$('#search').on('keyup',function() {
var value = $(this).val();
//ADD THIS... ++
if (value.length === 0) {
//clear results
$('#display').text('');
//no query, just return.
return;
}
//++
$.ajax({
type : 'GET',
url : '/search',
data:{'search': value},
success:function(data) {
if (data == '\0') {
$('#display').text('');
}
$('#display').html(data).show();
//alert(data.success);
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.