• Resolved Mattmcca

    (@mattmcca)


    Hello, I am in need of some guidance or help, I would like to populate a hidden field from a dropdown field. The list includes a list of usernames that are obtained from the DB, I would like to populate the hidden field with the same email address obtained from the database. This email address will be used to send an email to this user

    • This topic was modified 1 year, 6 months ago by Mattmcca.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @mattmcca,

    I hope you’re doing well.

    Could you please share more details on the dropdown field which you’d like to copy the email address from. Is it part of the same Forminator form, or another plugin (which one)?

    If the dropdown is part of a different plugin or a custom form, please also send its CSS class/ID if possible, a page URL, and any other relevant info.

    If the dropdown field has been created in Forminator, could you please remove any sensitive data (emails, etc.), export the form(s), and share them using a service like Google Drive or Pastebin.com

    Here’s how to export forms in Forminator:
    https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export

    Best Regards,
    Dmytro

    Thread Starter Mattmcca

    (@mattmcca)

    Hello
    https://drive.google.com/drive/folders/1V02RxLSng0CUhqjnMeeu0KlqJm_OGjPJ?usp=share_link

    I would like to create a select statement that gets the email of the user and populate it in Hiddenfield2 , i have attached both the form and the code that i am using. Is there an action that be called please

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @mattmcca

    Thank you for response!

    I checked your form and code. If you want to get user email into the select field, all that is needed is a small change in the code that you are already using. You’d jus tned to change these lines

    'label' => $user->user_login,
    'value' => $user->user_login,

    to this

    'label' => $user->user_login,
    'value' => $user->user_email,

    This way, the “display” of drop-down list would still be usernames but the option value for each of them would be e-mail address.

    Having them there, you could actually use that select field directly for sending Forminator e-mail notification (setting that select field as “Recipient”) without the need for any additional hidden field.

    If you still want to populate hidden field with the e-mail address from selected drop-down field, it’s a bit tricky and I would recommend using regular “input” field instead of the “hidden” field. This is because hidden fields are now validated and while we could use JS to populate it with currently selected value of drop-down, that value would not be saved anyway – because it would not be set in hidden field configuration.

    Using a regular “input” field overrides that restriction.

    Then it can be populated from “Select” field with a bit of additional code (you can add it to your existing code file):

    add_action( 'wp_footer', 'forminator_populate_input_from_select', 99 );
    function forminator_populate_input_from_select() {
    	
    	?>
    	
    	<script type="text/javascript">
    	
    	jQuery(document).ready(function($){
        $('.populate-list select').on('change',function(){
             if($(this).val() != "default"){
                $('.hidden-populate input').val($(this).val());
            }else{
                $('.hidden-populate input').val('');
            }
        });
    });
    	
    	
    	</script>
    	
    	
    	<?php 
    }

    You would also need to:

    – in select field (the source field) set this as Additional CSS class in “Styling” section (note no dot at the beginning)

    populate-list

    – and in the input field set this as Additional CSS class in “Styling section (again, no dot at the front):

    hidden-populate

    To make that input field hidden, you can simply add following CSS to the site:

    .hidden-populate {display:none;}

    Note: the form must NOT have “Load form using AJAX” option enabled – it has to be disabled, otherwise the shared JS will not work correctly.

    —–

    That being said, if you want to NOT include email as a value of select field, then populate other field with e-mail fetched automatically in the background based on the username selected in select – that’s a whole different story and it would require more complex development, including AJAX requests to actually fetch user e-mail based on username; this would be out of the scope of this support, I’m afraid, so I hope proposed solution would work fine for you (I tested it on my end before sharing).

    Best regards,
    Adam

    Thread Starter Mattmcca

    (@mattmcca)

    Thank you this is what i needed, just note one thing i tired at first to use an input field but in the sending of the recipients, i was not able to add input field, I changed it to email and it worked

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Select Field Prepopulate Hidden Field’ is closed to new replies.