default:get and select2
-
Hello,
this plugin is perfect but one litte thing is missing. Is it possible to prefill selectbox with default:get by URL parameter?
Thanks ??
-
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 includeGermany
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.
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.
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.
Thank you very much. With this code snippet it is working ??
Hello @jannnfe
I’m completing a plugin update to use the directly the get defined into the SELECT tag.
Best regards.
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.
- The topic ‘default:get and select2’ is closed to new replies.