After using the same old Wordpress contact form plugin, FastSecureContactForm, and custom JavaScript code for years to populate a dropdown list in a contact form with file titles from an ACF field, I decided to rebuild my website. Now, I am using Contact Form 7 and would like to replicate this functionality.
The previous JavaScript code referred to the ACF field containing track names as track_name
and the form field in FastSecureContactForm where the list of file names was generated dynamically as si_contact_ex_field2_3
. The numbers indicated the specific form (2) and extra field (3). Here is a snippet of the JavaScript used:
<script type="text/javascript">
var field_to_update = document.getElementById('si_contact_ex_field2_3');
field_to_update.innerHTML = '';
var elOptNew = document.createElement('option');
elOptNew.text = 'Select'
elOptNew.value = 'Select';
field_to_update.add(elOptNew);
field_to_update.options[0].selected = true;
var track_names = document.getElementsByName('audioFileName');
for (i=0; i<track_names.length; i++) {
var track_name = track_names[i].innerHTML;
var elOptNew = document.createElement('option');
elOptNew.text = track_name.replace("&", "&");
elOptNew.value = track_name;
field_to_update.add(elOptNew);
}
I have two questions:
A) How can I adapt the above JavaScript to reference and populate the dropdown field in the new Contact Form 7 form with data from the ACF field? Currently, I have created a "Form Tag" like this
[select* menu-470 "Select" "Track A" "Track B" "Track C"]
, but I need the "Track A" "Track B" "Track C"
part to be dynamically substituted with multiple track_name
s retrieved from the ACF field.
B) If there is a simpler or better way to achieve dynamic population of the dropdown field in Contact Form 7, I would appreciate any suggestions or guidance.
Thank you in advance for your assistance!
Phil