Forums › Forums › Search & Filter Pro › Updating search data via AJAX not returning anything
Tagged: ajax, search data
- This topic has 1 reply, 1 voice, and was last updated 6 years, 6 months ago by Anonymous.
-
Anonymous(Private) April 20, 2018 at 10:12 pm #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, PhotographyHere 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 Activities0When 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) { } 0I’ve confirmed that the form ID is correct.
What am I missing here?Anonymous(Private) April 20, 2018 at 10:58 pm #173537Ok, 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 ); ?>
-
AuthorPosts