How do you change the email recipient of drupal 7 webform based on checkbox value using hook form alter? I have a group of checkboxes (Texas, DC, Maryland, Virginia). I would like to change the recipient if Texas is checked and use the default for the other options. I do not want to change the key of the checkbox component to the Texas email address because I need to state which location was selected in the email template.
2 Answers
You do not need to use hook_form_alter for this, you can set this up in Webform on admin, here is how:
- Go to the Edit interface of your webform
- Go to E-mails
- At the bottom, choose 'Component value' and choose your checkbox field, click Add http://cl.ly/image/1K3V2N3k3q30
- At 'Component e-mail options' you can specify an email address for each option http://cl.ly/image/1g3c1r15060r
Description below says:
"You may enter an e-mail address for each choice. When that choice is selected, an e-mail will be sent to the corresponding address. If a field is left blank, no e-mail will be sent for that option."
-
1Doesn't that require me to set the email as the key in the checkbox field? I am not able to specify email addresses for each option in step 4 as you outlined above.– mschfCommented Mar 24, 2014 at 17:24
-
No it doesn't, that's why I proposed this solution. I'm updating my answer with screenshots in a minute. Commented Mar 25, 2014 at 13:09
-
What version of webform and Drupal is this from? I am using webform 7.x-3.20 with Drupal 7.20. I do not have those options available. Nor do I have the tab that says conditionals (from the 2nd screenshot). Is there another module I need to install?– mschfCommented Mar 26, 2014 at 14:31
-
This is from 7.x-3.20 and I haven't installed anything else. Is your field a 'Select options' field? Commented Mar 26, 2014 at 18:15
-
1
This is one solution that works but I'm sure there is a more Drupal friendly way to do it.
I basically created a hidden input field called email recipient with the default value as the default email address. Then used jquery to change it's value depending on if the checkbox is checked or not.
$('#checkbox').click(function() {
$("input[name='submitted[email_recipient]']").val($(this).is(':checked') ? 'email1' : 'email2');
});
Then in Drupal admin under webform emails, set 'Email to' as the hidden input field. Also unchecked the field from the list of available fields in the email template so it doesn't get printed in the emails that get sent out.