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
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);
            }
        });
}

本文标签: phpAjax search box displays nothing if taxonomy doesn39t exist