Inserting arrays in submission from frontend form

Support MB Frontend Submission Inserting arrays in submission from frontend formResolved

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #23496
    marcodedomarcodedo
    Participant

    Hi, I have this situation: I want to create a form that can be submitted from front end, but among my fields I have a list field with values loaded dynamically from a array.
    If I look at the changes from the backend everything is ok, but if I look at the form from the frontend the field remains empty.

    Please see the example:

    $myArray = array('first', 'second', 'third');
    
    if ($myArray) {
        $mylist = array_combine($myArray, array_values($myArray));
    } else {
        $mylist = null;
    }
    
    add_filter( 'rwmb_meta_boxes', 'prefix_register_meta_boxes' );
    function prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array(
            'title'      => 'Personal Information',
            'post_types' => 'post',
            'id'         => 'myform',
    
            'fields' => array(
               array(
                    'name'    => 'List',
                    'id'      => 'MyList',
                    'type'    => 'checkbox_list',
                    
                    'options' => $mylist,
                ),
            )
        );
    
        return $meta_boxes;
    }

    I use this shortcode on the page
    [mb_frontend_form id="myform"]

    #23500
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The variable $mylist in the function prefix_register_meta_boxes() refers to a local version of the $mylist variable, and it has not been assigned a value within this scope so it will not render the list options.

    You can move the variable $myArray and $mylist into the function prefix_register_meta_boxes() to make it works.

    add_filter( 'rwmb_meta_boxes', 'prefix_register_meta_boxes' );
    function prefix_register_meta_boxes( $meta_boxes ) {
        $myArray = array('first', 'second', 'third');
    
        if ($myArray) {
            $mylist = array_combine($myArray, array_values($myArray));
        } else {
            $mylist = null;
        }
        $meta_boxes[] = array(
            'title'      => 'Personal Information',
            'post_types' => 'post',
            'id'         => 'myform',
    
            'fields' => array(
               array(
                    'name'    => 'List',
                    'id'      => 'MyList',
                    'type'    => 'checkbox_list',
                    
                    'options' => $mylist,
                ),
            )
        );
    
        return $meta_boxes;
    }

    Or use the global keyword. Get more details in the documentation https://www.php.net/manual/en/language.variables.scope.php.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.