• Resolved jemo97

    (@jemo97)


    Hi Forminator

    Thanks for a great plug-in!!

    I have an “event” custom post type and want to display it with a sign-up form. On the same page (outside the form) I have a radio button group where the user can select a date among three date options that I fetch from a meta field for the event. How can I submit the selected radio group value together with the form? I can’t seem to fetch the radio group value via a hidden field.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @jemo97,

    I hope you are doing well today!

    The following code snippet could be used as a mu-plugin to help you on the issue.

    <?php
    add_action('wp_footer', 'wpmudev_change_hidden_val', 9999);
    function wpmudev_change_hidden_val() {
        if ( ! is_page( 'your_page_slug' ) ) { //Please use your page slug
            return;
        }
        ?>
        <script type="text/javascript">
        jQuery(document).ready(function($){
            $( '#radio-1 input' ).on( 'change', function() {
                var radio_val = $(this).val();
                $('input[name="hidden-1"]').val(radio_val);
            });
        });
        </script>
        <?php
    }
    
    add_filter( 'forminator_prepared_data', 'wpmudev_add_hidden_field_val', 10, 2 );
    function wpmudev_add_hidden_field_val( $prepared_data, $module_object ){
    	if ( $module_object->id != 6 ) { //Please change the form ID
    		return $prepared_data;
    	}
    
    	if ( empty( $prepared_data['hidden-1'] ) ) {
    		$prepared_data['hidden-1'] = sanitize_text_field( $_POST['hidden-1'] );
    	}
    
    	return $prepared_data;
    }
    
    add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_data', 10, 3 );
    function wpmudev_change_hidden_field_data( $entry, $module_id, $field_data_array ) {
        $form_ids = array(6); //Please change the form ID
    	if ( ! in_array( $module_id, $form_ids ) ) {
    		return;
    	}
        
        foreach ( $field_data_array as $key => $value ) {
            if ( strpos( $value['name'], 'hidden-1' ) !== false ) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( $_POST[$value['name']] );
            }
        }
    
    }

    A few notes on the code snippet;
    your_page_slug?should be replaced with your page slug
    6?should be replaced with your form’s ID
    radio-1?should be changed to radio field ID
    hidden-1?should be changed to hidden field’s ID.

    You can find more information below on how to use mu-plugins.
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins
    and
    https://www.ads-software.com/support/article/must-use-plugins/

    Kind regards,
    Zafer

    Thread Starter jemo97

    (@jemo97)

    Thank you I’ll try it out!

    Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi again @jemo97,

    We haven’t heard from you in a while, we will go ahead and mark this thread as resolved. If you have any additional questions or require further help, please let us know!

    Kind regards,
    Zafer

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Fetch radio button value from outside form’ is closed to new replies.