Skip Navigation

[Resolved] Preserve query string on ajax search

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a custom search View with several search fields placed on a page. Only the search form, not the results, are shown on the page. The View is set up to redirect to a different URL, where the filters and results are both displayed. When Users visit the first page, the URL will have a parameter "promocode" with some variable. When the page redirects to the next step, I want to maintain the original URL parameter on the results page.

Solution: Use the View filter "wpv_filter_end_filter_form" to add a hidden input field to the custom search form, and set the value of the field by accessing the URL parameter in the $_GET superglobal:

add_filter( 'wpv_filter_end_filter_form', 'prefix_add_hidden_field', 99, 4 );
function prefix_add_hidden_field( $out, $view_settings, $view_id, $is_required ) {
    $views = array( 16855 );
    if ( in_array( $view_id, $views) && $is_required ) {
      $promocode = isset($_GET['promocode']) ? $_GET['promocode'] : '';
      $out = '<input type="hidden" id="promocode" name="promocode" value="' . $promocode . '" />' . $out;
    }
    return $out;
};

Then if your View is set up to "only show available inputs" you'll need to add some custom JavaScript to update the hidden promocode field any time the filters are updated:

jQuery( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) {
    var url = new URL(window.location);
    var promocode = url.searchParams.get("promocode");
    jQuery('#promocode').val(promocode);
});

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_end_filter_form

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 11 replies, has 2 voices.

Last updated by Sammut 1 year, 11 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#1127088

We have a URL like this:

/disney-cruise-line-quote-form/disney-cruise-line-quote-form-step-2/?promocpde=swvcarib

that has a query string that must remain in the URL. However, when we perform an ajax search (no page refresh) the query string disappears. How can we preserve the query string?

#1127103

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Okay that's a bit unusual, existing parameters should persist. Please go to Toolset > Settings > Frontend Settings and tell me whether either checkbox in Browser History Management is checked. Then take a screenshot of the View editor screen's Custom Search settings panel so I can see your search configurations. If the View is online somewhere, please provide a link.

#1127468

Yes, thank you, those are all checked off.

If you want to see this in action go to: hidden link

Select No --> Continue

On the next page is where we are filtering and we need the query string preserved for when we submit the form again to the next page.

Screenshot attached. Thank you!

#1127517

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Okay I see what you mean. When a search form and its results are not shown on the same page, the existing URL parameters are not persisted. We offer a PHP API that will allow you to add a hidden field to the search filters. We can set the value of that hidden field to be the same as the current promocode URL parameter. When the page redirects, the hidden field will be added to the new URL parameters. Please add this code to your custom child theme or code snippets:

add_filter( 'wpv_filter_end_filter_form', 'prefix_add_hidden_field', 99, 4 );
function prefix_add_hidden_field( $out, $view_settings, $view_id, $is_required ) {
    $views = array( 16855 );
    if ( in_array( $view_id, $views) && $is_required ) {
      $promocode = isset($_GET['promocode']) ? $_GET['promocode'] : '';
      $out = '<input type="hidden" id="promocode" name="promocode" value="' . $promocode . '" />' . $out;
    }
    return $out;
}
#1127582

Thank you! It kind of worked. It appends the name of the variable, but not the value:

hidden link

#1127662

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Check the URL parameter name, it's inconsistent across the 3 URLs. Not sure how your redirect to step two is being applied, but that URL parameter is 'promocpde', on step one and step three the URL param is 'promocode'. If you adjust it on step two to be promocode, the snippet should work.

#1127750

I did double check. The function does work when you first hit the page, but one search is clicked the hidden variable goes empty.

Viewing source I can also see the Toolset it attempting to preserve the query string but it leaves it blank. Could this be a bug?

Thanks!!

#1128280

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Okay it looks like there was one piece of code I overlooked, and it's necessary if you're using the "only show available inputs" view option. Underneath the Search and Pagination panel, toggle open the JS editor and add this code to update the promocode input field value after the form is updated:

jQuery( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) {
    var url = new URL(window.location);
    var promocode = url.searchParams.get("promocode");
	jQuery('#promocode').val(promocode);
});
#1128364

Thanks for all of that. I did play with the jQuery part before to no avail and similarly, the url and form variables are not sticking. I can see that toolset is attempting to append them but no luck. 🙁

#1128446

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Transposition error on my part, my input field had a different ID and I did not update it correctly before pasting. I updated the code above to fix that, can you try once more?

#1128451

Oh, I saw that and already adjusted it and the query string would not hold.

#1128484

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

It just worked for me up to step 3, I'm attaching a screenshot here. Are you saying it should persist through step 4 as well? Or is there a specific step-by-step process I should follow to reproduce the problem on step 3?