• Resolved alwanma27

    (@alwanma27)


    Hello Dear,

    Hope this message finds you well.

    I have a question about the select field in a form. How can i know what the user selected value is before form is submitted. In other words, is there a way to get the selected value when a user presses on one of the options.

    Thank you
    Best regards

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

    (@wpmudevsupport15)

    Hi @alwanma27,

    I hope you are doing well today!

    Please provide us more details on what you would like to achieve, if you want to perform some operations based on selected values, then you can use conditional logic features.

    https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#Forminator-Conditional-Logic

    Kind regards,
    Zafer

    Thread Starter alwanma27

    (@alwanma27)

    Hello Zafer,

    Hope this message finds you well.

    I can’t use conditional logic in my case because what I want to make is: select field A is filled with the name of some company and based on what the user select in select A I have to generate from the database select B field option. For example, The user choose X company in select field A then select B field should be filled with the phone number of staff of this company where the phone number are restored from the database.

    Thank you
    Best regards

    Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @alwanma27,

    Forminator does not have such an option out of the box. The following workaround can help with populating the select field dynamically. Please check if that helps.

    add_filter( 'forminator_cform_render_fields', function( $wrappers, $model_id ) {
        if( $model_id != 361 ){
            return $wrappers;
        }
    
        $select_fields_data = array(
            'select-1' => 'users',
        );
    
        foreach ( $wrappers as $wrapper_key => $wrapper ) {
            if ( ! isset( $wrapper[ 'fields' ] ) ) {
                continue;
            }
    
            if ( isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) && ! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) ) {
                $users = get_users();
    
                if ( ! empty( $users ) ) {
                    $new_options = array();
    
                    foreach( $users as $u ) {
                        $new_options[] = array(
                            'label' => $u->user_login,
                            'value' => $u->user_login,
                            'limit' => '',
                            'key'   => forminator_unique_key(),
                        );
                    }
    
                    $wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options;
                }
            }
        }
    
        return $wrappers;
        
    },10,2);
    
    add_filter( 'forminator_replace_form_data', function( $content, $data, $original_content ) {
        if( $data['form_id'] != 361 ){
            return $content;
        }
    
        if ( ! empty( $content ) ) {
            return $content;
        }
    
        $form_fields = Forminator_API::get_form_fields( $data['form_id'] );
        $data_field = '';
        foreach($data as $key => $value){
            if ( strpos( $key, 'select' ) !== false ) {
                $values = '';
                $field_value = isset( $data[ $key ] ) ? $data[ $key ] : null;
    
                if ( ! is_null( $field_value ) ) {
                    $fields_slugs  = wp_list_pluck( $form_fields, 'slug' );
                    $field_key     = array_search( $key, $fields_slugs, true );
                    $field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] )
                            ? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' )
                            : array();
    
                    if ( ! isset( $field_options[ $field_value ] ) && isset( $_POST[ $key ] ) ) {
                        return sanitize_text_field( $_POST[ $key ] );
                    }
                }
            }
        }
    
        return $content;
    
    },10,3);

    Note: Please change 361 to your form’s ID in the code. Also, we are assuming the select field’s ID as select-1 please change that too if it’s different.

    You can add the code using a mu-plugin. Please find more details here: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    We recommend testing this on the dev/staging version first before putting it on the live site.

    Please feel free to get back to us if you need any clarification.

    Kind Regards,
    Nebu John

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @alwanma27 ,

    We haven’t heard from you for some time now, so it looks like you don’t need our assistance anymore.

    Feel free to re-open this ticket if needed.

    Kind regards
    Kasia

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