Inserting arrays in submission from frontend form
Support › MB Frontend Submission › Inserting arrays in submission from frontend formResolved
- This topic has 1 reply, 2 voices, and was last updated 4 years, 12 months ago by
Long Nguyen.
-
AuthorPosts
-
December 9, 2020 at 4:19 PM #23496
marcodedo
ParticipantHi, 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"]December 9, 2020 at 9:45 PM #23500Long Nguyen
ModeratorHi,
The variable
$mylistin the functionprefix_register_meta_boxes()refers to a local version of the$mylistvariable, and it has not been assigned a value within this scope so it will not render the list options.You can move the variable
$myArrayand$mylistinto the functionprefix_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
globalkeyword. Get more details in the documentation https://www.php.net/manual/en/language.variables.scope.php. -
AuthorPosts
- You must be logged in to reply to this topic.