Forums

Email a user when a checkbox is ticked

jpipitone 18 Feb, 2011
We are using Chronoforms to host a New Employee for that I built.

My question is, when a user ticks a checkbox such as "Product Name Needed," it will automatically email someone depending on which email address we assign to that checkbox.

Is there a plugin that exists that has this functionality, or is there a built in function?

Currently, we're using Dynamic To for a text field. Is it as simple as adding another Dynamic To and assigning that to the checkbox that is ticked? Can we do this for multiple checkboxes?

Thank you
GreyHead 18 Feb, 2011
Hi jpipitone,

Please check FAQ #31 which has the code for doing this with a drop-down, it needs modifying a little to work with radio buttons or checkboxes but the basic method is the same.

Bob
jpipitone 25 Feb, 2011
Ok - I've read the FAQ and made the changes to my on submit code - before sending email.

According to this FAQ, it says I must include a select name = recipients in my html.

I don't want the user to have to pull down email options, I simply want these all hidden.

If a user ticks a certain checkbox labeled "Adobe," then it must email someone specific in our company who is responsible for Adobe.

If a user ticks a checkbox labeled "Microsoft," then it must email the Microsoft person at our company.

Where would I specify the recipients depending on which checkbox is ticked?

As far as this goes:

$MyFormEmails->setEmailData(1, 'to', $emails_2[$_POST['recipients']]);

-> this is easily done if I am using select name = "recipients" form code, but I am actually using checkbox form code:

<input type="checkbox" name="Adobe" value="yes" id="Adobe" />


and just below that is:

<input type="checkbox" name="Microsoft" value="yes" id="Microsoft" />


Also, the value is currently set to "yes," however according to the faq, I need to set the value equal to em1, em2, etc.

Can you help?
jpipitone 25 Feb, 2011
So I've done a little thinking, but I still can't seem to get this to work right.

I've modified this code for the checkboxes to:

<input type="checkbox" name="adobe" value="yes" id="adobe" />


I've added this onto some existing php code on the on submit before sending email.

$emails_2 = array('Adobe'=>'Adobecontact@ourcompany.com');
$MyForm =& CFChronoForm::getInstance('employeeinfo');
$MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
$MyFormEmails->setEmailData(1, 'to', $emails_2[$_POST['adobe']]);

$emails_3 = array('Microsoft'=>'Microsoftcontact@ourcompany.com');
$MyForm =& CFChronoForm::getInstance('employeeinfo');
$MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
$MyFormEmails->setEmailData(1, 'to', $emails_3[$_POST['microsoft']]);


Something still doesn't seem to be right, it only wants to send the email out to the Adobe contact, not the Microsoft contact. I've tried checking one or the other, as well as both... I'll revisit this on Monday.
GreyHead 26 Feb, 2011
Hi jpipitone,

You seem to be making this a bit complicated.

Instead of the recipients drop-down use a check-box array (you can use any name here but I'll stay with 'recipients'. In the Form HTML:

<input type="checkbox" name="recipients[]" value="adobe" id="recipients_adobe" />
<input type="checkbox" name="recipients[]" value="microsoft" id="recipients_adobe" />
. . .

This will return an array in the form results.

Note if you have ChronoForms set to handle arrays for you you'll need to recovert the string to an array before the next step.

In the OnSubmit before box:
<?php
$email_array = array (
  'adobe' => 'adobe_expert@example.com',
  'microsoft' => 'microsoft_man@example.com',
  . . .
  'other' => 'catch_all@example.com'
);

$recipients = JRequest::getVar('recipients', array('other'), 'post', 'array');
$to_array = array();
foreach ($recipients as $v ) {
 $to_array[] = $email_array[$v];
}
$to_array = implode(',', $to_array);
$MyFormEmails->setEmailData(1, 'to', $to_array);
?>
NB Not tested and may need debugging!

This should send a copy of the email to each 'expert' identified in the checkbox array - with a fall-back to the catch-all address if nothing was checked.

Bob
jpipitone 26 Feb, 2011
OK, I'll give this a shot.

One other thing - in your post, we're setting the name="recipients[]" however in the form results that get emailed, our results on the form include the actual checkbox name, so we know which checkbox was selected.

I'll have to see if your code changes that, if it does, I'll have to figure out another way to email proper results.

Thank you for your help either way!
GreyHead 26 Feb, 2011
Hi jpipitone,

It should be OK as the *values* of the checkboxes are now 'adobe', 'microsoft', etc. You can convert the $recipients array into a string for display with code like this in the OnSubmit Before Email box
<?php
$product_list = implode(', ', $recipients);
JRequest::setVar('product_list', $product_list);
?>
and then use
Products: {product_list}
in the Email Template.

Bob
jpipitone 02 Mar, 2011
Hmm...seems to be partially working. It only sends an email to the first email that I put into the array.

I noticed there might be a typo here, so I added a single quote to correct it, and it stops sending email completely.


'microsoft' => microsoft_man@example.com',


to

'microsoft' => 'microsoft_man@example.com',


Before correcting the syntax, I'm getting an error after submitting the form on my thank you page, however the email gets sent out. After correcting it, I no longer get the error, but the email doesn't get sent.

Parse error: syntax error, unexpected '@', expecting ')' in /usr/share/ourintranet.com/components/com_chronocontact/libraries/customcode.php(64) : eval()'d code on line 4
jpipitone 02 Mar, 2011
Scratch that - it seems to be working.

Your help is greatly appreciated!
jpipitone 02 Mar, 2011
OK - so the emails are being sent correctly.

One problem - in my email template, I have the following fields setup:

To: [email]helpdesk@ourdomain.com[/email]
Dynamic To: HiringManagerEmail
Subject: Form
CC: [email]someoneelse@ourdomain.com[/email]
Fromname: Intranet
FromEmail: [email]email@ourdomain.com[/email]

If one of the checkboxes is selected, the form seems to be ignoring the Dynamic To now - it is not being sent to our helpdesk email anymore.

Any ideas why that would be happening?
GreyHead 02 Mar, 2011
Hi jpipitone,

Sorry about the typo - I've gone back and fixed my earlier post.

For the To problem the Helpdesk is in the static To, not the Dynamic To?

The code we have at present
$MyFormEmails->setEmailData(1, 'to', $to_array);
is setting the value of the To box and so is over-writing anything that is in there already.

The easy answer is to add the value that you always want included into the array here
$to_array = array();
$to_array[] = 'helpdesk@ourdomain.com'; // <!-- add this line
foreach ($recipients as $v ) {

Bob
jpipitone 02 Mar, 2011
Works great - your support is awesome.

Where can I make a donation?
GreyHead 02 Mar, 2011
Hi jpipitone,

Great to hear that it works :-)

There's a 'buy me a beer link' in my sig below (not that I actually drink much beer) if you feel so inclined. Thanks for the thought.

Bob
This topic is locked and no more replies can be posted.