JS to Set Default Values Based On Value of Another Field

By: Eric Gurry | Asked: 12/16/2022
ForumsCategory: Code HelpJS to Set Default Values Based On Value of Another Field
Eric Gurry asked 1 year ago
I am trying to set the default values for a given field in a Formidable form based on the value of another field in the same form.  Here is a simplified description.  Field2 is a checkbox field with options drawn from Woocommerce product categories.  I want to set the default entries in Field2 based on the value of Field1, which has the URL of the referring page ([server param="HTTP_REFERER"]).  So if Field1 contains "xyz", then Field2 should have default values of "1,2,11,12". Otherwise, Field2 should have default values of "1,2". I tried doing this using an if/then statement in the Default Value box for Field2 (for example, {if {get:field-1} == "xyz"}1,2,11,12{else}1,2{/if}), but have come to learn that Formidable conditional calculations can only be used for integers, not strings.  I am hoping to get some guidance on some JavaScript code that can accomplish my objectives. Thanks in advance for any help you can offer.
1 Answers
Victor Font answered 1 year ago
What values are in the referrer that you want to reference that decides what to check in field2? If you're using query string parameters, you can base conditionals on the [get param] shortcode. Second, how you you populating the checkbox values? You can't set default values for a standard checkbox using the default value field on the field's advanced tab. You set checkbox default values by clicking on the default values in the form builder. https://formidableforms.com/knowledgebase/using-dynamic-default-values-in-fields/#kb-add-a-default-value-to-a-checkbox-field. Please provide more detail about what you're actually trying to accomplish.
Eric Gurry replied 1 year ago

The checkboxes for Field2 are dynamically created from the taxonomy "Product categories". We are able to enter the default values for that field (without any conditions) in the Advanced->Default Value section without problem by specifying the number of the category rather than the category description. That part works fine. It is trying to make those values conditional that isn't working.

Regarding the value in the referring URL, I am trying to match a text string that could be present in that URL. So if the URL is blahblah.com/products/coats/product21/, I might be looking for the word "coats" to be present in the URL to assign one set of default categories, otherwise I would assign another set of default categories.

Does that answer your questions?

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right