最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Ajax search box displays nothing if taxonomy doesn't exist

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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);
            }
        });
}
发布评论

评论列表(0)

  1. 暂无评论