Forums

If Statements to direct which email a form goes to?

vward 20 Jul, 2009
Hello!

I have created a tech help form. One of the sections is a set of radio buttons that offers a building location. I would like to have the email sent to different people based on which radio button is selected. Is there a way to do that? On our current website we have something like this:

if radio=1 send email to [email]user1@domain.com[/email] and CC [email]admin@domain.com[/email]
if radio=2 send email to [email]user2@domain.com[/email] and CC [email]admin@domain.com[/email]
if radio=3 send email to [email]user3@domain.com[/email] and CC [email]admin@domain.com[/email]
if radio=4 send email to [email]user4@domain.com[/email] and CC [email]admin@domain.com[/email]

IS there a way to do that with chronoforms? PLEASE TELL ME YES.

~Valerie WArd
nml375 20 Jul, 2009
Hi Valerie,
This is easily done with CF, and is documented in the FAQ#31.

Simply put though; assuming you've named your radio input "location", the on-submit (before email) code would look something like this:
<?
$MyForm =& CFChronoForm::getInstance('form_name_here');
$MyEmail =& CFEMails::getInstance($MyForm->formrow->id);

switch (JRequest::getInt('location', -1)) {
  case 1:
    $MyEmail->setEmailData(1, 'to', 'user1@domain.com');
    break;
  case 2:
    $MyEmail->setEmailData(1, 'to', 'user2@domain.com');
    break;
  case 3:
    $MyEmail->setEmailData(1, 'to', 'user3@domain.com');
    break;
  case 4:
    $MyEmail->setEmailData(1, 'to', 'user4@domain.com');
    break;
  default:
    $MyEmail->setEmailData(1, 'to', 'admin@domain.com');
    return;
}
$MyEmail->setEmailData(1, 'cc', 'admin@domain.com');
?>

You could use different conditional structures to 'switch' of course, merely a habit of mine. This should however illustrate the process (code is written for 3.1 RC and newer, see the FAQ#31 for older versions of CF).

/Fredrik
MrSchadow 07 Jan, 2010
You guys are great!
I spent 3 hours yesterday trying to make this work. I followed the instructions in FAQ #31 and couldn't get it to work for 2 hours. Then I found you guys and had it working in less than 10 minutes. The only difference in mine was that instead of radio buttons, I made a drop down list to choose from.
Someone should put your code in the FAQs instead of that one in #31.
I'm just starting with CF, so I'm sure I'll need more help, and I know I'll be well received.🙂
This topic is locked and no more replies can be posted.