Conditional checkboxes

annietechwriter 08 Sep, 2010
Hello,

Is it possible to include an "if/then" conditional type statement within a form that sends a different email depending on a user's answer?

What I am trying to do is: If a user selects a 'Yes' checkbox to opt in to promotional emails they need to get an automatically generated email that verifies that they want our promotions. If they select 'No' they may or may not get a different email, depending on what the form is all about.

Can I do this through the code (or in any way)? I've searched through the forum and have seen similar questions, but none that quite match what I am looking for.

Thank you in advance.
annietechwriter 09 Sep, 2010
Thanks for the link. I'll give that a try and see how it goes.

In just glancing through the code though, I see that emails have to be disabled and they are activated through the php code. How is it determined which email is being called? I'll have the normal admin email, a generic 'thank you' email, then the 'opt in' email. Are they just numbered in the order they are presented in the form?

Again, thank you for your help and I'll be buying the book very soon.
annietechwriter 09 Sep, 2010
I made a few changes to my form - I now have only one 'YES" check box that is either selected or it isn't. When it is selected, I get the appropriate email (YEA!). When it is not selected, I do not get an email (YEA!). So it looks like that part works fine.

But now have a different issue:

I need two emails to be sent regardless of whether the 'YES' check box is selected or not. One email goes to the administrator with all the form information, the other goes to the email provided on the form, thanking them for their submission. Currently, if I enable these two emails, two instances of each one is delivered. If I disable them, I get none. Should I be adding something to the "On Submit code - after sending email"? code this is what I have:

<?php
$formname = 'EOS';
$MyForm =& CFChronoForm::getInstance($formname);
$MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
if(JRequest::getVar('check0')){
$MyFormEmails->setEmailData(1, 'enabled', 1);
}
$MyFormEmails->sendEmails($MyForm, $MyFormEmails->emails);
?>

Thanks again for your help.
GreyHead 10 Sep, 2010
Hi annietechwriter,

We need to think through the logic here. The extra code you've added is triggering the sending of *all* the emails. ChronoForms doesn't have a selective version of
$MyFormEmails->sendEmails($MyForm, $MyFormEmails->emails);


So you need to disable all of your emails to prevent them being sent automatically and then re-enable them in this code block. The first '1' in here is the Email id
$MyFormEmails->setEmailData(1, 'enabled', 1);
So if your other emails are 2 & 3 (in the order they are shown in the setups) your code will need to be.
<?php
$formname = 'EOS';
$MyForm =& CFChronoForm::getInstance($formname);
$MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
if ( JRequest::getVar('check0') ) {
    $MyFormEmails->setEmailData(1, 'enabled', 1);
}
$MyFormEmails->setEmailData(2, 'enabled', 1);
$MyFormEmails->setEmailData(3, 'enabled', 1);
$MyFormEmails->sendEmails($MyForm, $MyFormEmails->emails);
?>
Then all three emails should be sent correctly.

Bob
annietechwriter 15 Sep, 2010
Thanks again. I haven't had time to try it, but I'll let you know how everything comes out.
annietechwriter 16 Sep, 2010
That solved my problem! Thank you so much.
I need to take this a step further, but I'll start a new post.
This topic is locked and no more replies can be posted.