Title: Saving user selection from HTML shortcode to database
Last modified: November 5, 2022

---

# Saving user selection from HTML shortcode to database

 *  Resolved [gareth94](https://wordpress.org/support/users/atebol/)
 * (@atebol)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/)
 * I’m using the following code to save the contents of my HTML element into my 
   hidden field. However, clicking Submit on the form only submits the shortcode
   itself (`{filter_cm_tooltip_parse}`) to the database and not the selected value
   of the select field generated in the shortcode?
 *     ```
       // Render shortcode on HTML fields
       add_filter( 'forminator_replace_variables', 'do_shortcode' );
       // Include shortcode output to hidden field
       add_filter( 'forminator_custom_form_submit_field_data', function( $field_data_array ){
   
           $form = array(
               'field' => 'hidden-1', // Form hidden field
               'shortcode' => '{filter_cm_tooltip_parse}' // Shortcode to be rendered
           );
   
           foreach( $field_data_array as &$field_single ){
               if( $field_single['name'] === $form['field'] ){
                   $field_single['value'] = strip_tags( do_shortcode( $form['shortcode'] ) );
               }
           }
   
           return $field_data_array;
       });
       ```
   

Viewing 15 replies - 1 through 15 (of 26 total)

1 [2](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/page/2/?output_format=md)

 *  Plugin Support [Patrick – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport12/)
 * (@wpmudevsupport12)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16170541)
 * Hi [@atebol](https://wordpress.org/support/users/atebol/)
 * I hope you are doing well.
 * From what I can see the code is correct,
 * I did it by testing the following:
 *     ```
       <?php
   
       // Render shortcode on HTML fields
       add_filter( 'forminator_replace_variables', 'do_shortcode' );
   
       function test_of_shortcode_hidden_field(){
   
           ob_start();
           $current_user = wp_get_current_user();
           if($current_user->ID == 0){
               echo "<span class='usr-id'>No logged user</span>";
           }else{
           ?>
           <?php 
               echo $current_user->ID ?>
           <?php
           }
           return ob_get_clean();
   
       }add_shortcode('show-me-id','test_of_shortcode_hidden_field');
   
       // Include shortcode output to hidden field
       add_filter( 'forminator_custom_form_submit_field_data', function( $field_data_array ){
   
           $form = array(
               'field' => 'hidden-1', // Form hidden field
               'shortcode' => '[show-me-id]' // Shortcode to be rendered
           );
   
           foreach( $field_data_array as &$field_single ){
               if( $field_single['name'] === $form['field'] ){
                   $field_single['value'] = strip_tags( do_shortcode( $form['shortcode'] ) );
               }
           }
   
           return $field_data_array;
       });
       ```
   
 * And using the [shortcode] structure.
 * Had you tested the strip_tags( do_shortcode( {filter_cm_tooltip_parse} ) ) on
   a regular page to verify if it will output any content too?
 * Best Regards
    Patrick Freitas
 *  Thread Starter [gareth94](https://wordpress.org/support/users/atebol/)
 * (@atebol)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16175075)
 * Thank you [@wpmudevsupport12](https://wordpress.org/support/users/wpmudevsupport12/)
   for your response 🙂 Apologies if my initial question wasn’t clear. I can get
   the shortcode displaying perfectly fine on the page. The issue I have is the 
   fact that I can’t detect the user’s selection from the HTML dropdown when they
   click ‘Submit’ on the form… Once they click Submit, I would like their selection
   to be stored to the database. Thank you
    -  This reply was modified 3 years, 7 months ago by [gareth94](https://wordpress.org/support/users/atebol/).
 *  Plugin Support [Nithin – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport11/)
 * (@wpmudevsupport11)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16185157)
 * Hi [@atebol](https://wordpress.org/support/users/atebol/),
 * I tested the snippet Patrick shared, and it adds the content from the shortcode
   upon submission. Sounds like your issue is more specific to the shortcode.
 * Is it a custom shortcode? or from the plugin? If custom code, then please do 
   share the code for the shortcode used.
 * Also share the form export so that we could have a better idea. Please check 
   the following doc on how to export a form:
    [https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export](https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export)
 * If you are concerned about any sensitive information in the form, then you can
   duplicate your form, remove any sensitive information, and then export it.
 * You can share the export file via Google Drive, Dropbox or any cloud services
   in the next reply.
 * Looking forward to your response.
 * Kind Regards,
    Nithin
 *  Thread Starter [gareth94](https://wordpress.org/support/users/atebol/)
 * (@atebol)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16191009)
 * Thank you [@wpmudevsupport11](https://wordpress.org/support/users/wpmudevsupport11/)
   for your support. I’ve found the code in the [following thread](https://wordpress.org/support/topic/how-can-i-get-data-from-my-external-sql-database-into-the-select-field/)(
   posted by [@wpmudev-support9](https://wordpress.org/support/users/wpmudev-support9/))
   that seems to do exactly as I need. However, the custom values for the dropdown
   aren’t displaying on the frontend? When I click the dropdown, I just see the 
   text ‘No results found’?
    -  This reply was modified 3 years, 7 months ago by [gareth94](https://wordpress.org/support/users/atebol/).
 *  Thread Starter [gareth94](https://wordpress.org/support/users/atebol/)
 * (@atebol)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16192632)
 * Also, could you tell me how I can show/hide fields based on the dropdown selection
   please? The Visibility tab doesn’t seem to detect/recognise the options generated
   by a MySQL query in the selection field? Thank you
 *  [Kris – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport13/)
 * (@wpmudevsupport13)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16199297)
 * Hi [@atebol](https://wordpress.org/support/users/atebol/)
 * Are you able to export your:
    – form – full custom snippet you use and upload
   to google drive or dropbox so that we could take a closer look at this?
 * As for Visibility you can update select field options for the backend also through
   [https://wpmudev.com/docs/api-plugin-development/forminator-api-docs/#method-update_form_field](https://wpmudev.com/docs/api-plugin-development/forminator-api-docs/#method-update_form_field).
   Once values are rendered can be used for conditional logic as well.
 * Kind Regards,
    Kris
 *  Thread Starter [gareth94](https://wordpress.org/support/users/atebol/)
 * (@atebol)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16201625)
 * Hi Kris. Thanks so much for your help with this. I’ve included the form export
   and my full snippet code in the [following folder on Google Drive](https://drive.google.com/drive/folders/1gBlSrSKZ0-TMj7ykpeWgZWx_3e0YvMSR?usp=share_link).
   Thank you again
 *  Plugin Support [Laura – WPMU DEV Support](https://wordpress.org/support/users/wpmudev-support8/)
 * (@wpmudev-support8)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16204294)
 * Hi [@atebol](https://wordpress.org/support/users/atebol/)
 * Thanks for response!
 * I’ve checked what you shared and I admit I’m even more confused now. Let’s take
   a step back then and try to “break that down” a bit.
 * 1. Initially you reported that the hidden field contains shortcode itself instead
   of its output.
 * i was wondering where this `{filter_cm_tooltip_parse}` shortcode is coming from
   since you didnt’ specify it and I finally found it. it’s a shortcode that is 
   a “custom wrapper” for a `{cm_tooltip_parse}` shortcode of CM Tooltip plugin 
   and, apparently the code that makes it was provided by us.
 * However, I’m not sure what’s the goal here. This shortcode is meant to display
   some sort of a tooltip and it will not work in submissions anyway. Furthermore,
   the code that you shared in initial post uses “forminator_custom_form_submit_field_data”
   forminator filter – this is a filter that’s applied “in background” upon submission.
   It doesn’t “display anything” and cannot make any JS to be executed and/or apply
   any front-end CSS to the submission data (and original cm_tooltip_parse seems
   to depend on JS or CSS if I correctly understand).
 * Ultimately, I’m not sure what’s the goal of trying to add a tooltip into a hidden
   field data, especially “upon submission”.
 * Would you explain this a bit more, please?
 * 2. The custom code that you shared recently
 * This code is meant to fetch data from a 3rd-party/external database, from specific
   tables. It needs to have DB connection configured and use correct DB tables names.
   It’s my understanding that you did correctly configure DB connection (just removed
   from shared code – which is fine) but
 * – are tables names correctly set in these lines of the code?
 *     ```
       $result = $conn->query("SELECT wdt_ID, clientname FROM wp_wpdatatable_2 ORDER BY clientname ASC");
                    $result2 = $conn->query("SELECT wdt_ID, type FROM wp_wpdatatable_3 ORDER BY type ASC");
                    $result3 = $conn->query("SELECT wdt_ID, status FROM wp_wpdatatable_4 ORDER BY status ASC");
                    $result4 = $conn->query("SELECT wdt_ID, name FROM wp_wpdatatable_1 WHERE activeuser = 1 ORDER BY name ASC");
       ```
   
 * – are select field IDs configured to match form select fields in these lines?
 *     ```
       private $field_id   = 'select-3';
               private $field_id2   = 'select-4';
               private $field_id3   = 'select-5';
               private $field_id4   = 'select-2';
       ```
   
 * – and how does it related at all to the initial question about shortcode?
 * Please advise!
 * Kind regards,
    Adam
 *  Thread Starter [gareth94](https://wordpress.org/support/users/atebol/)
 * (@atebol)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16212694)
 * Thank you [@wpmudev-support8](https://wordpress.org/support/users/wpmudev-support8/)
   for your response 🙂 Please ignore my initial code (at the beginning of this 
   thread). The updated/current code is what I’ve shared in the Google Drive link,
   [here](https://drive.google.com/drive/folders/1gBlSrSKZ0-TMj7ykpeWgZWx_3e0YvMSR).
   The issue I have is that selecting an option from the dropdown boxes and submitting
   the form yields the following error for each of the dropdowns: ‘Selected value
   does not exist.’
 *  Plugin Support [Laura – WPMU DEV Support](https://wordpress.org/support/users/wpmudev-support8/)
 * (@wpmudev-support8)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16218724)
 * Hi [@atebol](https://wordpress.org/support/users/atebol/)
 * Thanks for response!
 * I’ve tested this and you’re right – it does result in error. I’m pretty sure 
   this is due to the changes in plugin that would make this code require some modifications(
   note that it’s a custom code) but I wasn’t able to get it solved yet.
 * I need to consult it with out developers so I’ve asked them directly to check
   it again and see if they come up with necessary modification.
 * I’d appreciate some patience as they are dealing with a lot of complex tasks 
   on daily basis and their response time might be a bit longer than ours here. 
   But we’ll update you here again as soon as we got feedback on this.
 * Kind regards,
    Adam
 *  Plugin Support [Nithin – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport11/)
 * (@wpmudevsupport11)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16221635)
 * HI [@atebol](https://wordpress.org/support/users/atebol/),
 * Could you please try this snippet and then check whether it works?
 * [https://cloudup.com/files/iSYHxSFkn-e/download](https://cloudup.com/files/iSYHxSFkn-e/download)
 * You can apply the above snippet as a mu-plugins. Please check this link on how
   to implement the above code as a mu-plugins:
    [https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins](https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins)
 * Kind Regards,
    Nithin
 *  Thread Starter [gareth94](https://wordpress.org/support/users/atebol/)
 * (@atebol)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16237255)
 * Hi [@wpmudevsupport11](https://wordpress.org/support/users/wpmudevsupport11/).
   Thank you for your support. I’ve tried your code but the same thing happens 🙁
   I get a ‘Selected value does not exist’ error for each of the dropdowns. I have‘
   Store submissions in database’ selected under the form settings. I’ve double 
   checked the database but the data has not been submitted.
 *  Plugin Support [Patrick – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport12/)
 * (@wpmudevsupport12)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16237365)
 * Hi [@atebol](https://wordpress.org/support/users/atebol/)
 * Sorry to hear the issue persists, we pinged our Second Line support to verify
   once again the code for you.
 * Best Regards
    Patrick Freitas
 *  Plugin Support [Laura – WPMU DEV Support](https://wordpress.org/support/users/wpmudev-support8/)
 * (@wpmudev-support8)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16252041)
 * Hi [@atebol](https://wordpress.org/support/users/atebol/)
 * It seems there’s been a bit of misunderstanding on our end, I’m sorry about that.
 * When asking our developers for help, I have earlier modified the code slightly
   because I don’t have the same setup/db-structure. Apparently my colleagues didn’t
   notice that when sharing updated code with you. Once again, I’m sorry about that.
 * Could you give that code another try, please?
 * Just make sure to change this part
 *     ```
       // $conn = new mysqli('localhost', 'DB_NAME', 'DB_USER', 'DB_PASS') or die ('Cannot connect to db');
   
                   // $result = $conn->query("SELECT wdt_ID, clientname FROM wp_wpdatatable_2 ORDER BY clientname ASC");
                   // $result2 = $conn->query("SELECT wdt_ID, type FROM wp_wpdatatable_3 ORDER BY type ASC");
                   // $result3 = $conn->query("SELECT wdt_ID, status FROM wp_wpdatatable_4 ORDER BY status ASC");
                   // $result4 = $conn->query("SELECT wdt_ID, name FROM wp_wpdatatable_1 WHERE activeuser = 1 ORDER BY name ASC");
   
   
       			$result['wdt_ID'] = 1;
       			$result['clientname'] = "Client 1";
       			$result2['wdt_ID'] = 2;
       			$result2['type'] = "Type 2";
       			$result3['wdt_ID'] = 3;
       			$result3['status'] = 'Status 3';
       			$result4['wdt_ID'] = 4;
       			$result4['name'] = 'Name 4';
       ```
   
 * to this
 *     ```
       $conn = new mysqli('localhost', 'DB_NAME', 'DB_USER', 'DB_PASS') or die ('Cannot connect to db');
   
       $result = $conn->query("SELECT wdt_ID, clientname FROM wp_wpdatatable_2 ORDER BY clientname ASC");
       $result2 = $conn->query("SELECT wdt_ID, type FROM wp_wpdatatable_3 ORDER BY type ASC");
       $result3 = $conn->query("SELECT wdt_ID, status FROM wp_wpdatatable_4 ORDER BY status ASC");
       $result4 = $conn->query("SELECT wdt_ID, name FROM wp_wpdatatable_1 WHERE activeuser = 1 ORDER BY name ASC");
       ```
   
 * replacing DB_NAME, DB_USER and DB_PASS with correct credentials of your DB.
 * Also in this part make sure that form ID and fields IDs are correct:
 *     ```
       // User defined settings
               private $form_id    = 2153;
               private $field_id   = 'select-3';
               private $field_id2  = 'select-4';
               private $field_id3  = 'select-5';
               private $field_id4  = 'select-2';
       ```
   
 * Kind regards,
    Adam
    -  This reply was modified 3 years, 6 months ago by [Laura - WPMU DEV Support](https://wordpress.org/support/users/wpmudev-support8/).
      Reason: db creds
 *  Plugin Support [Laura – WPMU DEV Support](https://wordpress.org/support/users/wpmudev-support8/)
 * (@wpmudev-support8)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/#post-16252192)
 * Hi again [@atebol](https://wordpress.org/support/users/atebol/)
 * Just in case you’d need to download code again, please use this link (changes
   described in my last post above still would need to be made; I’m sorry for complication):
 * [https://cloudup.com/files/ifHCBqoqtAu/download](https://cloudup.com/files/ifHCBqoqtAu/download)
 * Best regards,
    Adam

Viewing 15 replies - 1 through 15 (of 26 total)

1 [2](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/page/2/?output_format=md)

The topic ‘Saving user selection from HTML shortcode to database’ is closed to new
replies.

 * ![](https://ps.w.org/forminator/assets/icon-256x256.gif?rev=3443182)
 * [Forminator Forms – Contact Form, Payment Form & Custom Form Builder](https://wordpress.org/plugins/forminator/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/forminator/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/forminator/)
 * [Active Topics](https://wordpress.org/support/plugin/forminator/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/forminator/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/forminator/reviews/)

## Tags

 * [html](https://wordpress.org/support/topic-tag/html/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 26 replies
 * 6 participants
 * Last reply from: [Nithin – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport11/)
 * Last activity: [3 years, 1 month ago](https://wordpress.org/support/topic/saving-user-selection-from-html-shortcode-to-database/page/2/#post-16677472)
 * Status: resolved