admin管理员组文章数量:1336589
Hi i have a ajax search box that pulls all my taxonomies from my post type but if I start typing and the taxonomy does not exist it just displays a small box with nothing in it. is it possible to change this so if the taxonomy does not exist it pops up in the box saying 'no search results found'
this is my function:
function fetch(){
var keyword = jQuery('#listing_tags').val();
var myDiv = document.getElementById("ajaxbox");
if (keyword != "")
{
myDiv.style.display = "block";
} else {
myDiv.style.display = "none";
}
jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>',{'action':'my_action','keyword': keyword},
function(response){
jQuery('#datafetch').html('');
jQuery('#datafetch').append(response);
});
}
jQuery(document).on('click','#datafetch li',function(){
var val = jQuery(this).html();
jQuery('#listing_tags').val(val);
jQuery('#datafetch').html('');
var myDiv = document.getElementById("ajaxbox");
myDiv.style.display = "none";
});
Hi i have a ajax search box that pulls all my taxonomies from my post type but if I start typing and the taxonomy does not exist it just displays a small box with nothing in it. is it possible to change this so if the taxonomy does not exist it pops up in the box saying 'no search results found'
this is my function:
function fetch(){
var keyword = jQuery('#listing_tags').val();
var myDiv = document.getElementById("ajaxbox");
if (keyword != "")
{
myDiv.style.display = "block";
} else {
myDiv.style.display = "none";
}
jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>',{'action':'my_action','keyword': keyword},
function(response){
jQuery('#datafetch').html('');
jQuery('#datafetch').append(response);
});
}
jQuery(document).on('click','#datafetch li',function(){
var val = jQuery(this).html();
jQuery('#listing_tags').val(val);
jQuery('#datafetch').html('');
var myDiv = document.getElementById("ajaxbox");
myDiv.style.display = "none";
});
Share
Improve this question
asked May 20, 2020 at 12:05
JoeColdJoeCold
11 silver badge2 bronze badges
1 Answer
Reset to default 0You can always check the response. I don't know what your php function returns but I assume it returns nothing while not found. Lets modify the fetch()
function to check whether response is empty or not. From the code bellow you will get the idea of where to implement the popup.
function fetch() {
var keyword = jQuery('#listing_tags').val();
var myDiv = document.getElementById("ajaxbox");
if (keyword != "") {
myDiv.style.display = "block";
} else {
myDiv.style.display = "none";
}
jQuery.post('<?php echo admin_url('
admin - ajax.php '); ?>', {
'action': 'my_action',
'keyword': keyword
},
function(response) {
jQuery('#datafetch').html('');
if(response.trim()==''){
//This code will run when response is empty
jQuery('#datafetch').append('<p>no search results found</p>');
}else{
//This code will run if there is valid response
jQuery('#datafetch').append(response);
}
});
}
本文标签: phpAjax search box displays nothing if taxonomy doesn39t exist
版权声明:本文标题:php - Ajax search box displays nothing if taxonomy doesn't exist 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742410858a2469756.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论