• Resolved renplainview

    (@renplainview)


    Hi. I want the drop down menu of CF7 to have a default value, depending on the page the user is on.

    Details:
    I’m using the Select and Multi-Select Field for Contact Form 7 plugin (https://www.ads-software.com/plugins/select-multiselect-field-contact-form-7/) which creates drop down menus with a search field.

    Here’s an example of one of my form-tags:
    [selct_multiselct* number placeholder:--- "1" "2" "3" "4" "5"]

    I also have two pages, the WordPress excerpt (post_excerpt) for the page url.com/this-is-page-1 is “1” and the WordPress excerpt (post_excerpt) for the page url.com/this-is-page-2 is “2”.

    Instead of “- – -” placeholder, I want the drop down menu default value to be set to “1” when page url.com/this-is-page-1 is loaded, based on the fact that the post_excerpt for that page is “1”. After all, your plugin has the ability to use data from the post_exceprt (https://cf7-datasource.dwbooster.com/documentation#post-data).
    And when page 2 is loaded, the default value for the drop down menu should be “2” etc.

    Can your plugin help with that? What should my above form-tag look like? And perhaps some additional form-tags should be added to the form (recordset, recordset field link)?

    Thank you!

    • This topic was modified 2 years, 9 months ago by renplainview.
Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @renplainview

    Thank you very much for using our plugin.

    Your “select” field has the structure:

    [selct_multiselct* number placeholder:--- "1" "2" "3" "4" "5"]

    And you want to select the option whose value matches with the current post excerpt (the post where the form is inserted).

    To read the current post_excerpt you should use the recordset structure:

    [cf7-recordset id="cf7-recordset-414" type="post" attributes="post_excerpt" condition="ID={post.id}"]

    The condition ID={post.id} tell the plugin you want to filter the data by the current post’s id.

    Finally, to select the corresponding option in the multi-select field, you should subscribe to the recordset event.

    Note that you don’t want to replace the values in the multi-select field but only to select its choices.

    <script>
    document.addEventListener('cf7-recordset', function(evt){
        if(evt.detail['recordset-id'] == 'cf7-recordset-414')
        {
            jQuery('[name="number"] option[value="'+evt.detail['recordset-data'][0]['post_excerpt']+'"]').prop('selected', true).change();
        }
    }); </script>

    The complete structure would be:

    [selct_multiselct* number placeholder:--- "1" "2" "3" "4" "5"]
    [cf7-recordset id="cf7-recordset-414" type="post" attributes="post_excerpt" condition="ID={post.id}"]
    <script>
    document.addEventListener('cf7-recordset', function(evt){
    if(evt.detail['recordset-id'] == 'cf7-recordset-414')
    {
    jQuery('[name="number"] option[value="'+evt.detail['recordset-data'][0]['post_excerpt']+'"]').prop('selected', true).change();
    }
    }); </script>

    Best regards.

    Thread Starter renplainview

    (@renplainview)

    Thank you for the detailed response. It turned out that everything only works fine if you are logged into the WordPress admin. If you check from incognito, there is an error in the console:

    VM1774:2 Uncaught ReferenceError: cf7_datasource_register_recordset is not defined
        at <anonymous>:2:1
        at (index):3:5388
        at new Promise (<anonymous>)
        at RocketLazyLoadScripts._transformScript ((index):3:5080)
        at async RocketLazyLoadScripts._loadScriptsFromList ((index):3:5481)

    You can see it yourself at https://studibucht.de/test-page/. CF7 is at the bottom of the page, the “Arbeitstyp” field should display “Masterarbeit” by default when the page loads. This is what happens when you are logged in.

    We are using WPRocket and I thought at first it was all about jQuery, I specifically turned off all restrictions for jQuery. But it still doesn’t work.
    Could you suggest a solution to this problem?

    • This reply was modified 2 years, 9 months ago by renplainview.
    Plugin Author codepeople

    (@codepeople)

    Hello @renplainview

    Could you please modify the settings of WP Rocket? Specifically the way it loads the javascript resources, and purge its cache.

    Please, visit your website with the nocache parameter for testing:

    https://studibucht.de/test-page/?nocache=1

    Best regards.

    I have the same problem using wp-rocket.
    We have to exclude some of the scripts form the js delay of wp rocket.
    But after long testing, i cant find out the right settigs.
    i think the problem is the inline script containing f7_datasource_register_recordset. I think this should not be a inline script that it will working with wp rocket.

    Best regards.

    Plugin Author codepeople

    (@codepeople)

    Hello @jannnfe

    You can exclude the inline scripts from WP Rocket settings:

    Best regards.

    Hello,

    Unfortunately that doesn’t help me to solve the problem. Do you have any other idea what to add to the exclusion list?

    Best regards.

    Plugin Author codepeople

    (@codepeople)

    Hello @jannnfe

    Could you please indicate the URL to the page to check if your website is loading the plugin resources in the correct order?

    Best regards.

    Hey,

    yes the URL is: https://umbracanis.de/selbstauskunft/

    Thanks ??

    Plugin Author codepeople

    (@codepeople)

    Hello @jannnfe

    Please, look at the source code of your page:

    <script data-minify="1" type='text/javascript' src='https://umbracanis.de/wp-content/cache/min/1/wp-content/plugins/cf7-data-source/assets/script.js?ver=1657120463' id='cf7_datasource_script-js' defer></script>
    <script type='text/javascript' id='cf7_datasource_script-js-after'>
    cf7_datasource_register_recordset("hundenamen-list", {"fields":[],"variables":[],"cf7":2399}, 194)
    cf7_datasource_register_link({"recordset":"hundenamen-list","field":"tv_name","value":"post_title","text":"post_title","condition":"","limit":"","default":[],"fields":[],"variables":[]}, 194)
    </script>

    The code in the inline script should work after loading the script.js file, but “WP Rocket” ignores the Scripts dependency.

    To fix this issue, please, configure the “Defer” options in the “WP Rocket” to not apply the defer to the “cf7-data-source/assets/script.js” script.

    Best regards.

    Thanks! I added the following in WPRocket defer exclude and now its working:

    /jquery-?[0-9.](.*)(.min|.slim|.slim.min)?.js
    /cf7-data-source/assets/script(.min)?.js

    Best regards.

    • This reply was modified 2 years, 8 months ago by jnnfx.
    • This reply was modified 2 years, 8 months ago by jnnfx.
    Plugin Author codepeople

    (@codepeople)

    Hello @jannnfe

    Excellent !!! Thank you very much for sharing your solution.

    Best regards.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Getting dynamical default values for CF7 drop-down menu’ is closed to new replies.