Contact Form7 is a well known wordpress plugin which is used mostly to develop a contact/query form. We can use all type of fields in contact form and need to assign values static. But there is no option of dynamically adding the values to fields in contact Form7 plugin. Here I am describing the way of dynamically adding options to select tag in Contact Form7.
If We use select tag of Contact Form7 then we need to include it as follows:
1 | [select country id:country class:dycountry include_blank] |
Now If I want to add the countries as options of this select tag and I can not give it as static. So we will use jQuery or JavaScript for dynamically adding options to select tag.
1 2 3 4 5 6 7 8 9 | <script> jQuery(document).ready(function(){ // it has all the option tag with country names // directly printing those options in select tag jQuery('#country').html(<?php echo $country ?>); }) </script> |
If we pass country list as an array then we need to loop through all the values by adding one by one option.
1 2 3 4 5 6 7 8 9 10 | <script> jQuery(document).ready(function(){ var countries = <?php echo $country ?> // it is array of countries // directly printing those options in select tag for(i=0;i'+countries[i]+''); } }) </script> |
There are many ways to add options to select tag in jQuery/javascript. We can use any of the way according to our knowledge and specified values with the conditions.
As above given examples, we can pass select options by fetching from database or any file in php and make possible dynamically adding options to select tag in Contact Form7.