Updating search data via AJAX not returning anything

Forums › Forums › Search & Filter Pro › Updating search data via AJAX not returning anything

Tagged: ,

Viewing 2 posts – 1 through 2 (of 2 total)
  • Anonymous
    #173536

    I have a filter form that automatically submits as the user selects filter parameters, and displays loads via AJAX. That’s working fine but I can’t seem to get the search data to return the correct values when the search results update.

    I would like to output something like this:

    Travel Month: January
    Destinations: Grand Canyon, Las Vegas
    Activities: Great for Kids, Photography

    Here is my JS script:

    (function($){
    	$(document).on("sf:ajaxfinish", ".searchandfilter", function(){
    		console.log("ajax complete");
    		$.ajax({
    		    type: "POST",
    		    url: "/wp-admin/admin-ajax.php",
    		    data: { action: 'filterParams'}
    		}).done(function( response ) {
    		    $( ".parameters" ).html( response );
    		});	 
    	});    
    })(jQuery);

    And here is the PHP script that I put in functions.php:

    function filterParams(){
        global $searchandfilter;
    	$sf_current_query = $searchandfilter->get(12192)->current_query();
    	$args = array(
    		"str" 					=> '%1$s: %2$s', // this is the format of the string generated for each field
    		"delim" 				=> array(", ", " - "), 
    		"field_delim"			=> '<br />',
    		"show_all_if_empty"		=> true
    	);
    	
    	echo $sf_current_query->get_fields_html(
    		array("_sft_travelmonth","_sft_destination", "_sft_activities"), 
    		$args
    	);
    }
    add_action('wp_ajax_filterParams', 'filterParams');
    add_action('wp_ajax_nopriv_filterParams', 'filterParams');

    This is what is always getting returned in the AJAX call:

    Travel Months: All Travel Months
    Destinations: All Destinations
    Activities: All Activities0

    When I use:
    var_dump($sf_current_query->get_array());
    instead of:

    echo $sf_current_query->get_fields_html(
        array("_sft_travelmonth","_sft_destination", "_sft_activities"), 
        $args
    );

    It returns an empty array:
    array(0) { } 0

    I’ve confirmed that the form ID is correct.
    What am I missing here?

    Anonymous
    #173537

    Ok, turns out I was way overengineering this. The simple solution is to put he PHP block INSIDE the element that the AJAX results load into. Like this:

    <div  id="ajax-results">
    <?php
    
    	global $searchandfilter;
    	$sf_current_query = $searchandfilter->get(12192)->current_query();
    	echo $sf_current_query->get_fields_html(
    		array("_sft_travelmonth","_sft_destination", "_sft_activities"), 
    		$args
    	);
    ?>
Viewing 2 posts – 1 through 2 (of 2 total)

Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.