Populate Select2 with Cfdb-submissions
-
Great plugin, but I’m stuck with using select2.
I’m using Cfdb-plugin and I’m trying to make select2 get values id, text and text2 from cfdb-database but only show id as option in select2. I need to populate hidden fields rstext and rstext2 with data-values text and text2 so that when the form is submitted, id, text and text2 will get saved to another table in their own columns (Cfdb will handle saving all fields at submit).
My table looks like this:
| id | text | text2 | rownumber | ----------------------------------------------- | rs1 | rs1text | rs1text2 | 1 | | rs2 | rs2text | rs2text2 | 2 | | rs3 | rs3text | rs3text2 | 3 |
In my functions.php I have:
// cfdb shortcode add_action('wpcf7_init', 'add_cf7_shortcode_select_rs'); function add_cf7_shortcode_select_rs() { wpcf7_add_form_tag('select_rs', 'select_rs', true); } // cfdb-select function select_rs($tag) { $tag = new WPCF7_FormTag( $tag ); $form = 'RS-form'; $options = array(); $options['orderby'] = 'rownumber DESC'; require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php'); $exp = new CFDBFormIterator(); $exp->export($form, $options); ob_start(); echo '<select name="select_rs[]" multiple="multiple" id="select_rs" class="rsselect js-example-basic-multiple wpcf7-form-control wpcf7-select">'; echo '<option value=""></option>'; while ($row = $exp->nextRow()) { printf('<option data-id="%s" data-text="%s" data-text2="%s">%s</option>', $row['id'], $row['text'], $row['text2'], $row['id']); } echo '</select>'; $custom_tag=ob_get_contents(); ob_end_clean(); return sprintf('<span class="wpcf7-form-control-wrap %s">%s</span>', sanitize_html_class( $tag->name ), $custom_tag); }
Create a Form Drop-Down with values from CFDB submissions
Also I would like to get select2-multiple selected and saved data with line breaks like this:
| id | text | text2 | ----------------------------------- | rs1 | rs1text | rs1text2 | | | moretext | | | rs2 | rs2text | rs2text2 | | | | moretext | ----------------------------------- | rs3 | rs3text | rs3text2 | | rs4 | rs4text | rs4text2 | ----------------------------------- | rs2 | rs2text | rs2text2 |
How to do this with select2 in your plugin?
- The topic ‘Populate Select2 with Cfdb-submissions’ is closed to new replies.