Forums

Generate different emails based on radio button

momentis 27 Jun, 2011
I have read through the posts in the forum relating to this topic, but I am unfortunately still confused! 😶

I have a form whose first field is a radio selection. The end user will select whether or not they will be participating in a trip. If they are going, the form will display many fields for entering the traveler's information. If they are not going, the form will display only a few fields.

If the user is going on the trip, the email sent to both the user and the company should contain all of the applicable fields on the form. If they are not going, the email to both the user and the company should only contain the limited number of fields.

I have created 4 emails for the form. They are as follows:
1 - Going on Trip: email generated to company, with all applicable fields included
2 - Going on Trip: email generated to user, with all applicable fields included
3 - Not Going on Trip: email generated to company, with only limited fields included
4 - Not Going on Trip: email generated to user, with only limited fields included

I have included the following code in the onSubmit Before Sending Emails area:
<?php
$formname = JRequest::getVar('chronoformname');

if ( !$formname ) {
   $params   =& $mainframe->getPageParameters('com_chronocontact');
   $formname = $params->get('formname');
}

$MyForm =& CFChronoForm::getInstance("$trip_registration");
$MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);

$answer_array = array (
  'going' => 1,
  'going' => 2,
  'notgoing' => 3,
  'notgoing' => 4,
);
$radio = JRequest::getVar( 'radio0', '', 'post');
$radio = explode(',', $radio0);
echo '<div>$radio: '.print_r($radio0, true).'</div>'; // add this line
foreach ( $radio as $v ) {
  $v = $answer_array[$v];
  $MyFormEmails->setEmailData($v, 'enabled', '1');
}
?>


where 'going' and 'notgoing' are the values of the radio0 field.

When I execute the form, I receive an error message stating that at least one recipient email must be provided. I turned on debug and am attaching a screenshot of the message I receive.

Each of the four emails I defined above do have recipients defined. I apologize if I am being obtuse, but I just cannot get this to work!

Rick
GreyHead 27 Jun, 2011
Hi Rick,

The terms radio & radio0 get a bit confused in these lines
$radio = JRequest::getVar( 'radio0', '', 'post');
$radio = explode(',', $radio0);
echo '<div>$radio: '.print_r($radio0, true).'</div>'; // add this line
I think they should be
$radio = JRequest::getVar( 'radio0', '', 'post');
$radio = explode(',', $radio);
echo '<div>$radio: '.print_r($radio, true).'</div>'; // add this line

Hopefully then you'll see a value of $radio echo'd out - in your image it is empty.

Bob
momentis 27 Jun, 2011
That helped! It is working as I expected when sending the different email to the user submitting the data. However, it is not sending either email to the company. Here's what the debug screen shows:
momentis 27 Jun, 2011
I think I know what is happening, but I don't know how to correct it.

When the following statement is evaluated:

$answer_array = array (
  'Going' => 1,
  'Going' => 2,
  'Not Going' => 3,
  'Not Going' => 4,
);


the code initially sets $answer_array = 1. then it gets set to 2 before proceeding. 2 and 4 are the emails sent to the user, while 1 and 3 are the ones sent to the company. That is why, I believe, the emails are only being sent to the users (as that condition is evaluated last). So, any idea how I can change this so that if 'Going' is selected, both emails 1 & 2 are enabled, and if 'Not Going' is selected, 3 and 4 are enabled?
GreyHead 27 Jun, 2011
Hi Rick,

Brute force maybe
. . .
$radio = JRequest::getVar( 'radio0', '', 'post');
switch ($radio) {
 case 'going':
   $MyFormEmails->setEmailData(1, 'enabled', '1');
   $MyFormEmails->setEmailData(2, 'enabled', '1');
   break;
 case 'not_going':
   $MyFormEmails->setEmailData(3, 'enabled', '1');
   $MyFormEmails->setEmailData(4, 'enabled', '1');
   break;
}
?>

Bob
momentis 27 Jun, 2011
Oh man, that did it!!! I just needed to change the options to match mine ("Going" and "Not Going") and it worked perfectly.

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

VPS & Email Hosting 20% discount
hostinger