Support Forum
Support › MB Frontend Submission › Send email if checkbox is tickedResolved
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
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
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 );
?>
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
no it doesn't redirect to the page - goes to the user dashboard. - other emails seem to be sending fine
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
thank you so much that worked
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
Hi,
You can create a name or email field, mark it required, and include this value in the email content.
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;
}
} );
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 );
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 );
?>
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 );
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
sorry - I have worked out the html formatting - thanks again - really good/helpful support from you!!