Custom Fields
Re-Envisioned
Support MB Custom Table Linking form fields to custom table columns

  • Creator
    Topic
  • #24339
    Resolved Martin
    Participant

    Hi.

    I wonder if I could get some direction on this scenario.

    I have a form which I’m displaying using the shortcode [mb_frontend_form id=’job-details’].

    The form is created in Builder with:

    <?php
    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'        => esc_html__( 'Job Details', 'text-domain' ),
            'id'           => 'job-details',
            'post_types'   => ['page'],
            'context'      => 'normal',
            'priority'     => 'high',
            'fields'       => [
                [
                    'id'   => $prefix . 'JobID',
                    'type' => 'number',
                    'name' => esc_html__( 'Job ID', 'text-domain' ),
                    'tab'  => 'tab_job',
                ],
                [
                    'id'   => $prefix . 'ClientID',
                    'type' => 'number',
                    'name' => esc_html__( 'Client ID', 'text-domain' ),
                    'tab'  => 'tab_job',
                ],
                [
                    'id'   => $prefix . 'Start_Date',
                    'type' => 'date',
                    'name' => esc_html__( 'Start Date', 'text-domain' ),
                    'tab'  => 'tab_job',
                ],
                [
                    'id'   => $prefix . 'End_Date',
                    'type' => 'date',
                    'name' => esc_html__( 'End Date', 'text-domain' ),
                    'tab'  => 'tab_job',
                ],
                [
                    'id'            => $prefix . 'g_Sitters',
                    'type'          => 'group',
                    'fields'        => [
                        [
                            'id'   => $prefix . 'SitterID',
                            'type' => 'number',
                            'name' => esc_html__( 'Sitter ID', 'text-domain' ),
                        ],
                        [
                            'id'   => $prefix . 'Sitter_Start_Date',
                            'type' => 'date',
                            'name' => esc_html__( 'Start Date', 'text-domain' ),
                        ],
                        [
                            'id'   => $prefix . 'Sitter_End_Date',
                            'type' => 'date',
                            'name' => esc_html__( 'End Date', 'text-domain' ),
                        ],
                    ],
                    'clone'         => 1,
                    'default_state' => 'expanded',
                    'max_clone'     => 5,
                    'add_button'    => esc_html__( 'Add additional sitter cover', 'text-domain' ),
                    'tab'           => 'tab_sitters',
                ],
            ],
            'tab_style'    => 'default',
            'tab_wrapper'  => true,
            'table'        => 's4p_job',
            'tabs'         => [
                'tab_job'     => [
                    'label' => 'Job Details',
                    'icon'  => 'dashicons-calendar',
                ],
                'tab_sitters' => [
                    'label' => 'Assigned Sitters',
                    'icon'  => 'dashicons-businessman',
                ],
            ],
            'include'      => [
                'relation'  => 'OR',
                'user_role' => ['administrator'],
            ],
            'storage_type' => 'custom_table',
        ];
    
        return $meta_boxes;
    }

    As you can see I’m saving the data in a custom table with the code below and this works fine.

    add_action( 'init', 'prefix_create_table' );
    function prefix_create_table() {
        if ( ! class_exists( 'MB_Custom_Table_API' ) ) {
            return;
        }
        MB_Custom_Table_API::create( 's4p_job', array(
            'JobID' => 'INT NOT NULL',
            'ClientID'   => 'INT NOT NULL',
            'g_Sitters'   => 'VARCHAR(65535) NOT NULL',
            'Start_Date'   => 'DATE NOT NULL',
            'End_Date'   => 'DATE NOT NULL',
        ) );
    
    }

    What I’d like to be able to do is have the form populate with the data from the custom table based on a search.

    My search would be a simple list of all the rows in the table for now (SELECT *…) but I’ll later need to modify on other custom fields as well.

    Once I have the ID I’d then need my frontend form to be populated with the values from this table and any changes to the values made in the form be updated back to that row.

    I’d also like similar behaviour with user meta which I have working with my custom fields all saved against the logged in user in the standard table but would be better if I could save this date in a custom table too and have it connected to the form properly from there as well.

    Any help would be much appreciated.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #24354
    Long Nguyen
    Moderator

    Hi Martin,

    We have an extension that integrates Meta Box and SearchWP https://metabox.io/plugins/meta-box-searchwp-integrator/.

    It helps you to searching data on the custom table or even with serialized data (group).

    #24358
    Martin
    Participant

    Thanks Long.

    So SearchWP gives a way to do the searching part (which I can do easily enough with code as well). How do I go about getting a Frontend form to be populated with the values I get back?

    If we assume I’m getting values from the custom table and have them in variables can I link the form fields when the form opens with the values I have? If so, how do I go about doing that?

    Obviously if I save everything as user meta than the fields are already populated when I display the form using mb_user_profile_info and a submission results in an update. Now I want to have the same functionality for any other form.

    #24373
    Long Nguyen
    Moderator

    Hi,

    You can follow the documentation to get the field value from the custom table when having the variable $post_id

    $value = rwmb_meta( $field_id, ['storage_type' => 'custom_table', 'table' => $table_name], $post_id );
    echo $value;
    

    For example:

    <?php 
    $value = rwmb_meta( $field_id, ['storage_type' => 'custom_table', 'table' => $table_name], $post_id );
    ?> 
    <form>
      <label for="fname">First name:</label><br>
      <input type="text" id="fname" name="fname" value="<?php echo $value; ?>">
    </form>
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.