0

I have dynamic meta fields with multi select option each field contains select menu with multiple select feature user can add as much options as they can so i want to save it in the db but not sure how do i do that as it is multi dimensional array

<div class="fields-wrapper">
            <div class="field-row">
                <div class="field-group">
                    <label>Option Name</label>
                    <input type="text" class="text-field" name="ja_option[]" />
                </div>

                <div class="field-cond">
                    <label>Available For Meals</label>
                    //ja_meal_id[1][] the number 1 will be changed via javascript new field is added it will add +1 to this number so next row will be like this ja_meal_id[2][]...ja_meal_id[3][] and so on
                    <select name="ja_meal_id[1][]" class="input-text-box chosen-select av_meals" data-placeholder="Select Meals ..." multiple>
                        <?php if( $testimonials->have_posts() ) : ?>
                        <?php
                            while( $testimonials->have_posts() ) :
                                $testimonials->the_post();
                        ?>
                            <option value="<?php echo the_ID(); ?>"><?php echo the_title(); ?></option>
                        <?php 
                            endwhile;
                            endif;
                        ?>
                    </select>
                </div>
            </div>
        </div>

        <a class="add_field">+ Add Fields</a>

        <input type="hidden" name="countFields" value="0" /> //This is total number of rows

This is the backend of custom meta box saving fields

function rooms_details_save( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    if ( $parent_id = wp_is_post_revision( $post_id ) ) {
        $post_id = $parent_id;
    }

    $fields = [
        'ja_option',
        'ja_meal_id'
    ];

    foreach ( $fields as $field ) {
        if ( array_key_exists( $field, $_POST ) ) {
            update_post_meta( $post_id, $field, 
                is_array($_POST[$field]) ?
                    array_map( 'sanitize_text_field', $_POST[$field] )
                :
                    sanitize_text_field( $_POST[$field] ) 
            );
        }
    }
}

Now the problem which i am facing here is the field data is not saving either it is because I am not properly sending the name or it is not allowing multidimensional array fields to be acceptable please let me know what should I do and how should I save it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.