Send email if checkbox is ticked

Support MB Frontend Submission Send email if checkbox is tickedResolved

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #33337
    hello@twosixmedia.co.uk[email protected]
    Participant

    I want to send an email to an admin if the user ticks a checkbox to say that the form is complete.

    I'm using the submit button as a save the form button so that the user can continue to fill out the form as they wish - once completed I want the admin to be notified that's it's complete and can be checked by them to verify completion.
    I have added a checkbox yet - I want to know if it's possible first

    https://sfgqualitymark.org.uk/green-care-form/

    is this possible?

    thanks

    Paul

    #33343
    Long NguyenLong Nguyen
    Moderator

    Hi,

    It is possible. You can check the field value via the global variable $_POST and use the form action hook rwmb_frontend_after_process to send the email after the user submits the form.

    add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
        $value = empty( $_POST['field_id'] ) ? null : $_POST['field_id'];
        if ( 'yes' === $value ) {
            wp_mail( '[email protected]', 'New submission', 'A new post has been just submitted.' );
            wp_safe_redirect( 'thank-you' );
            die;
        }
    }, 10, 2 );

    Refer to the documentation https://docs.metabox.io/extensions/mb-frontend-submission/#form-actions

    #33346
    hello@twosixmedia.co.uk[email protected]
    Participant

    I have tried to set this up - but the email doesn't seem to be sending - what anm I doiung wrong? using a code snippets type of plugin for this as I'm using oxygen builder and no theme is used

    <?php
    
    add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
        $value = empty( $_POST['submit_form_for_review'] ) ? null : $_POST['submit_form_for_review'];
        if ( 'yes' === $value ) {
            wp_mail( '[email protected]', 'New submission', 'A Green Care
    Quality Mark Form has been just submitted for review.' );
            wp_safe_redirect( 'review' );
            die;
        }
    }, 10, 2 );
    
    ?>
    #33355
    Long NguyenLong Nguyen
    Moderator

    Hi,

    After submitting the form, does the function wp_safe_redirect() work, redirect to the review page? If yes, the function wp_mail() is not working due to some problem with SMTP on your hosting. You can contact your hosting support to fix this issue.

    Refer to this note in the WP documentation
    https://developer.wordpress.org/reference/functions/wp_mail/#notes

    #33359
    hello@twosixmedia.co.uk[email protected]
    Participant

    no it doesn't redirect to the page - goes to the user dashboard. - other emails seem to be sending fine

    #33372
    Long NguyenLong Nguyen
    Moderator

    Hi,

    If you use the checkbox field, the value submitted is 1 or 0. So you should compare it with this value.

    $value = empty( $_POST['submit_form_for_review'] ) ? null : $_POST['submit_form_for_review'];
    if ( $value == 1 ) {
    ...
    }

    Refer to this documentation https://docs.metabox.io/fields/checkbox/#template-usage

    #33373
    hello@twosixmedia.co.uk[email protected]
    Participant

    thank you so much that worked

    #33374
    hello@twosixmedia.co.uk[email protected]
    Participant

    can the email give any info the form submitted - who it was from or a link to it etc - at the moment the admin can't tell who submitted the form

    #33387
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can create a name or email field, mark it required, and include this value in the email content.

    #33401
    hello@twosixmedia.co.uk[email protected]
    Participant

    I;m sorry - I'm not sure how to add ther field to the email in the code you sent me - the post title is required - could we add that to the email that is sent?

    I added the code below to change it's name on the front end - not sure if that affects it

    thank you for your help so far

    Paul

    add_filter( 'rwmb_frontend_post_title', function( $field ) {
    if ( is_page( "Green Care Form" ) ) {
    $field['required'] = true;
    $field['name'] = 'Name of green care site';
    return $field;
    }
    } );

    #33404
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The code will look like

    add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
        $value = empty( $_POST['post_title'] ) ? null : $_POST['post_title'];
        wp_mail( '[email protected]', 'New submission', 'A new post has been just submitted from '. $value .' user' );
    }, 10, 2 );
    #33416
    hello@twosixmedia.co.uk[email protected]
    Participant

    the last code you sent doesn't check for the checkbox field for submission as in the code below

    I want it so that the email is sent when the user ticks the checkbox and the post title is included in the email so the admin know which post is ready for review

    sorry for all the questions on this but not a coder

    <?php

    add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
    $value = empty( $_POST['submit_form_for_review'] ) ? null : $_POST['submit_form_for_review'];
    if ( $value == 1 ) {
    wp_mail( '[email protected]', 'New submission', 'A Green Care Quality Mark Form has been just submitted for review.' );
    wp_safe_redirect( 'submitted' );
    die;
    }
    }, 10, 2 );

    ?>

    #33431
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can just combine to code to add the post title to email content.

    add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
        $value = empty( $_POST['submit_form_for_review'] ) ? null : $_POST['submit_form_for_review'];
        if ( $value == 1 ) {
            $value = empty( $_POST['post_title'] ) ? null : $_POST['post_title'];
            wp_mail( '[email protected]', 'New submission', 'A new post has been just submitted from '. $value .' user' );
            wp_safe_redirect( 'submitted' );
            die;
        }
    }, 10, 2 );
    #33432
    hello@twosixmedia.co.uk[email protected]
    Participant

    brilliant - thank you so much for all your help - works really well - one more question - is it possible to add html formatting to the email - like bold or headings etc

    regards

    Paul

    #33433
    hello@twosixmedia.co.uk[email protected]
    Participant

    sorry - I have worked out the html formatting - thanks again - really good/helpful support from you!!

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