Custom Fields
Re-Envisioned
Support General Struggling with button code actions

  • Creator
    Topic
  • #21411
    Resolved Martin
    Participant

    Hi.

    I’m sure I’m doing something really daft here (my coding is a bit rusty after a couple of decades break from getting my hands dirty).

    At the moment I’m simply trying to get a button on a form to have an action. I’m following this tutorial and I just get nothing happening when I click either button. https://metabox.io/add-custom-javascript-actions-using-meta-box/#more-19638

    I’m using the MB Builder to create a basic form as per the tutorial which gives me:

    <?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__( 'Button Code Test', 'text-domain' ),
            'id'     => 'button-code-test',
            'fields' => [
                [
                    'id'   => $prefix . 'whole_sale_price',
                    'type' => 'text',
                    'name' => esc_html__( 'Wholesale Price', 'text-domain' ),
                ],
                [
                    'id'   => $prefix . 'retail_price',
                    'type' => 'text',
                    'name' => esc_html__( 'Retail Price', 'text-domain' ),
                ],
                [
                    'id'   => $prefix . 'discount_price',
                    'type' => 'text',
                    'name' => esc_html__( 'Discount Price', 'text-domain' ),
                ],
                [
                    'id'   => $prefix . 'button_reset',
                    'type' => 'button',
                    'name' => esc_html__( 'Reset', 'text-domain' ),
                    'std'  => 'Reset',
                ],
                [
                    'id'   => $prefix . 'set_default',
                    'type' => 'button',
                    'name' => esc_html__( 'Default', 'text-domain' ),
                    'std'  => 'Default',
                ],
            ],
            'type'   => 'user',
        ];
    
        return $meta_boxes;
    }

    This is the code entered into functions.php for the TwentyTwenty theme which apart from having my forms has been unaltered (no other changes to functions.php etc):

    add_action( 'rwmb_enqueue_scripts', 'twentytwenty_enqueue_custom_script' );
    function twentytwenty_enqueue_custom_script() {
            wp_enqueue_script( 'script-id', get_template_directory_uri() . '/assets/js/admin.js', array( 'jquery' ), '', true );
    }

    I’m then adding the following into the admin.js file as described:

    jQuery( function ( $ ) {
    
    $( '#button_reset' ).on( 'click', function() {
                    $( '#button-code-test' ).find( 'input[type=text]' ).val('');
    } );
    
            $( '#set_default' ).on( 'click', function() {
                    $( '#button-code-test' ).find( '#whole_sale_price' ).val('150000');
                    $( '#button-code-test' ).find( '#retail_price' ).val('100000');
                    $( '#button-code-test' ).find( '#discount_price' ).val('80000');
            } );
    
    } );

    The admin.js file is located here: public_html/s4pshareportal.co.uk/wp-content/themes/twentytwenty/assets/js.

    The form is placed on a page using the shortcode [mb_frontend_form id=’button-code-test’] and can be seen on the page https://s4pshareportal.co.uk/

    I think the only thing I’ve done different to the tutorial is to have the form show on a page rather than a post but I guess I’ve done something else wrong as neither button does anything when I try it.

    Any hints would be much appreciated.

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

    Hi,

    The code that you are using is shown on the user profile page with the setting 'type' => 'user'. On the user profile page, the meta box does not have the ID like on the post/page (post type). You can change the JavaScript code to make it works on the user profile page.

    jQuery( function ( $ ) {
    
        $( '#button_reset' ).on( 'click', function() {
            $( 'body' ).find( 'input[type=text]' ).val('');
        } );
    
        $( '#set_default' ).on( 'click', function() {
                $( 'body' ).find( '#whole_sale_price' ).val('150000');
                $( 'body' ).find( '#retail_price' ).val('100000');
                $( 'body' ).find( '#discount_price' ).val('80000');
        } );
    
    } );

    Or remove the setting 'type' => 'user' to custom fields on the post editor and works with the frontend submission shortcode [mb_frontend_form id='button-code-test'].

    #21415
    Martin
    Participant

    Thanks for this.

    I have tried both options and still have no action happening in my form.

    I’ve first tried to change the code to replace my form name #button-code-test with body.

    I then reverted back to the original JavaScript and tried changing the Location setting from Users to Posts and set the post type to page.

    Neither of these worked. I also tried applying both changes and still no action is seen.

    Is there any way to tell even if the code is being run? Being new to this I’m not sure how I can see what actions are being triggered and what code is being run from a debugging perspective.

    #21419
    Long Nguyen
    Moderator

    Hi,

    After changing from #button-code-test to body, please try to clear the cache and check it again. You can see my screen record to know how to debug and check what code is running https://www.loom.com/share/8162539567a4408da6f8d76f51b0def3.

    #21427
    Martin
    Participant

    Awesome. Thanks for that. Of course it would be something silly and simple like the cache. I really should have thought of that myself – I’m clearly too old for all this 😉

    All is now working as hoped and I can crack on with all the easy bits (extracting data from the database, crafting it into JSON to send to another system, triggering some workflows etc).

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