• Resolved alwanma27

    (@alwanma27)


    Hello,

    I have a forminator pro form that contains three select fields. The select fields options should be filled from the database of other form submissions and also based on the option selected in the previous select field. For example, assume that I have select A, select B. Select A is filled by name of restaurant from the database then select B should be filled based on the selection made in select A and from the Database. So, My question is how can I make like an event listener to know which option is chosen in select A to be able to update the list of option of select B?

    Thank You
    Regards

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

    (@wpmudevsupport13)

    Hi @alwanma27

    I hope you are doing good today.

    I’m afraid this will be too complex and out of scope, but I asked our SLS Team to review your query.

    The only solution ATM will be to use Pre-Populate which will allow you to move values between pages and forms:
    https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#pre-populate-form-field-values

    Kind Regards,
    Kris

    Thread Starter alwanma27

    (@alwanma27)

    Hello Kris,

    I will try this in the meantime waiting the SLS Team to reply.

    Thank you
    Regards

    Plugin Support Dimitris – WPMU DEV Support

    (@wpmudev-support6)

    Hello there @alwanma27

    This looks a bit complex indeed, so this is what our developers proposed.

    We had a snippet to render users on select field. You can try modifying that and get the restaurant names from the DB and render on a Select A field but the second query looks too complex.

    Something like this:

    <?php
    add_filter( 'forminator_cform_render_fields', function( $wrappers, $model_id ) {
        if( $model_id != 361 ){
            return $wrappers;
        }
    
        $select_fields_data = array(
            'select-1' => 'restaurants',
        );
    
        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' ] ] ) ) {
    	    	$restaurant = 'Your_sql_query_to_get_restaurants';
    
                if ( ! empty( $restaurant ) ) {
                    $new_options = array();
    	       		$restaurant_names = array();
                    foreach( $restaurant as $restaurant_names ) {
                        $new_options[] = array(
                            'label' => $restaurant_names,
                            'value' => $restaurant_names,
                            'limit' => '',
                            'key'   => forminator_unique_key(),
                        );
    					$restaurant_names['options'] = $new_options;
                    }
    				$select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][0]['element_id'], true );
    				if( $select_field ){
    					if( count($select_field['options']) != count($restaurant_names['options']) ){
    						Forminator_API::update_form_field( $model_id, $wrapper['fields'][0]['element_id'], $restaurant_names );
    						$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'] );
        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);

    You will need to replace Your_sql_query_to_get_restaurants with the sql query and form ID from 361 to your form’s ID.

    Thank you,
    Dimitris

    Thread Starter alwanma27

    (@alwanma27)

    Hello Dimitris,

    Thank you for your response. I will try this.

    Thank you
    Regards

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Select options based on other selected option’ is closed to new replies.