[Forminator Pro] A bit of Forminator magic

Hey guys,

I hope I can explain this correctly which would lead to a yes and a description of how to do it :slight_smile:

I have a site that offers three services that require the user to fill out a form and it will email the submission. In the form the user will select what service they are interested in and then an actual interest within that service.

My question is – is it possible to set it up that if the user clicks on one of the services ( a button) that THAT service will be default selected in the form when it loads?

I currently have it three different forms on three different pages but surely there must be a smarter way as the rest of the form is identical.

Hope it makes sense.

Thanks

  • Dimitris
    • Support Star

    Hello there Mike,

    hope you’re doing well! :slight_smile:

    I could think of a workaround using a Hidden field with a HTTP Refer URL value in it, but this won’t track a specific button click, but rather the page URL where these buttons are.

    I’ve already pinged our Second Level Support team, in case there’s any other workaround we could use about that. We’ll keep you posted here as soon as possible.

    Thank you,

    Dimitris

  • Panos
    • SLS

    Hope you don’t mind using some custom snippet Baldafrican :slight_smile:

    You can try adding the following snippet in a mu-plugin (or your child theme’s functions.php):

    add_filter( 'forminator_field_single_markup', function( $html, $id, $required, $options ){

    //if ( 'select-1-field' != $id ) {
    // return;
    //}

    $query_var = 'fragment';
    $options_values = array_column( $options, 'value' );
    $value = false;

    if ( wp_doing_ajax() ) {

    $referer = wp_get_referer();
    $parsed = parse_url( $referer );
    parse_str( $parsed['query'], $query );
    $fragment = isset( $query[ $query_var ] ) ? $query[ $query_var ] : '';

    } else {
    $fragment = isset( $_GET[ $query_var ] ) ? $_GET[ $query_var ] : '';
    }

    if ( ! empty( $fragment ) && in_array( $fragment, $options_values ) ) {
    $value = $fragment;
    }

    if ( ! ! $value ) {
    // Remove default selected option
    $html = str_replace( 'selected="selected"', '', $html );
    // Replace selected item
    $html = str_replace( "option value="{$value}"", "option value="{$value}" selected="selected"", $html );
    }

    return $html;

    }, 20, 4 );

    That reads if a specific query var is set in the url. In the current example that query var is set to “fragment”, but you can change that according to your preference in this line:

    $query_var = 'fragment';

    In your Services links you can use the same page url, however you need to set the query var (eg fragment) to the value of the Select field’s option. The url should look like:

    http://site.com/the-form-page/?fragment=service-1

    Hope you find this handy :slight_smile:

    Kind regards!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.