Support » Plugin: Data Source for Contact Form 7 » default:get and select2

  • Resolved jannnfe

    (@jannnfe)


    Hello,
    this plugin is perfect but one litte thing is missing. Is it possible to prefill selectbox with default:get by URL parameter?
    Thanks 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @jannnfe

    Thank you very much for using our plugin.

    To select a default option, just include it as the only option in the SELECT field. It would be the option selected by default when the field is filled by the Recordset.

    I’ll try to describe the process with an example.

    Assuming you have a contact form that fills a menu (select) field with the country list stored in a CSV file:

    <label> Country list
        [select country-list] </label>
    
    [cf7-recordset id="country-data" type="csv" url="https://raw.githubusercontent.com/datasets/country-codes/master/data/country-codes.csv" headline="1" delimiter=","]
    
    [cf7-link-field recordset="country-data" field="country-list" value="official_name_en" text="official_name_en" condition="record['official_name_en']!=''"]

    To select Germany as the country by default. You should include Germany as the only option in the select tag: [select country-list "Germany"]

    The complete form would be:

    <label> Country list
        [select country-list "Germany"] </label>
    
    [cf7-recordset id="country-data" type="csv" url="https://raw.githubusercontent.com/datasets/country-codes/master/data/country-codes.csv" headline="1" delimiter=","]
    
    [cf7-link-field recordset="country-data" field="country-list" value="official_name_en" text="official_name_en" condition="record['official_name_en']!=''"]

    Best regards.

    Thread Starter jannnfe

    (@jannnfe)

    Hey,

    thanks for your quick reply. That’s not quite the answer to my question. I would like to describe my question a little better. sorry my mistake.

    This is my current situation:

    
    [select hundename id:sa-hundname class:form-select default:get select2]
    [cf7-recordset id="hundenamen-list" type="post" attributes="ID,post_title" condition="post_status='publish' AND post_type='vermittlungen'"]
    [cf7-link-field recordset="hundenamen-list" field="hundename" value="ID" text="post_title"]
    

    I don’t use the normal dropdown but the select2 dropdown with a search field. My goal now is that if a value is passed as a GET parameter in the URL, the dropdown automatically contains this value when the page loads. More info here: https://contactform7.com/getting-default-values-from-the-context/

    Example: https://domain.com/?hundename=12345
    Result on page load: The select2 dropdown should default to the option with the value from the GET parameter.

    It also seems that without select2 the function doesn’t work either, because as far as I know the dropdowns are filled with javascript after pageloading.

    I hope you can understand what i mean and maybe there is the possibility to make a small adjustment here.

    Thank you very much.

    Plugin Author codepeople

    (@codepeople)

    Hello @jannnfe

    Thank you very much for the clarification. In the current version of the plugin, you should use events. WCF7 tries to select the default option before the information is received into the Recordset field. So, the default attribute in the field’s setting would not take effect.

    Please, insert the following piece of code into your form.

    <script>
    jQuery('[name="hundename"]').on('change', function(evt, attr){
    if(attr == 'cf7-ds-fill') {
    const queryString = window.location.search;
    const urlParams = new URLSearchParams(queryString);
    const v = urlParams.get(this.name);
    jQuery(this).find('option[value="'+v+'"]').prop('selected', true);
    } 
    });
    </script>

    Our plugin triggers an on-change event when it fills a field, passing the cf7-ds-fill text as its second parameter.

    I’ll analyze how to support the “default” attribute in the [cf7-link-field] tags.

    Best regards.

    Thread Starter jannnfe

    (@jannnfe)

    Thank you very much. With this code snippet it is working 🙂

    Plugin Author codepeople

    (@codepeople)

    Hello @jannnfe

    I’m completing a plugin update to use the directly the get defined into the SELECT tag.

    Best regards.

    Plugin Author codepeople

    (@codepeople)

    Hello @jannnfe

    I’ve released the plugin update just now. This version supports the get attribute.

    With this version, you would not need the script code, only the tags structure:

    [select hundename id:sa-hundname class:form-select default:get select2]
    [cf7-recordset id="hundenamen-list" type="post" attributes="ID,post_title" condition="post_status='publish' AND post_type='vermittlungen'"]
    [cf7-link-field recordset="hundenamen-list" field="hundename" value="ID" text="post_title"]

    Best regards.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘default:get and select2’ is closed to new replies.