We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6180
    • 31 Posts
    I'm trying to implement a conditional emailTo for a contact form a user has requested.

    The form contains a list of interest options. Each interest will correspond to one of a number of email addresses that the message will need to be delivered to. e.g. Interest in the Food Bank will add the Social Services contact to the emailTo list, interest in the Senior Center will add that director to the list.

    I have made a snippet that is intended to parse the value list of the checkboxes and evaluate who gets added to the emailTo list, but it doesn't work.

    WhatToWho
    <?php
    if ($input == '') { return ''; }
      $flags = explode(' ',$input);
      foreach ($flags as $key => $value) {
        switch ($value)
        {
          case 'ao':
            $output[] = '[email protected]';
          case 'ss':
            $output[] = '[email protected]';
          default:
            $output[] = '[email protected]';
        }
      }
      return implode(', ', array_unique($output));


    Any ideas out there for how to do conditional emailTo values?
      • 3749
      • 24,544 Posts
      Is the snippet a postHook? In that case I think you'd want:

      $opts = $hook->getValue('name_of_checkbox_input');


      See this also: http://rtfm.modx.com/display/ADDON/FormIt.Handling+Selects,+Checkboxes+and+Radios


      You might also try adding something like this to see what you're getting:

      die($input);
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 6180
        • 31 Posts
        Thanks for the reply, Bob.

        The snippet is intended to be an output modifier. I have found that if I call the snippet directly on the page it works fine. However, when used as an output modifier I get no return at all - even if I strip the snippet to just this:
        return $input;


        The FormIt call looks like this:
        [[$pvcVolunteerForm]]
        [[!FormIt? &hooks=`email,redirect`
                   &emailSubject=`Volunteer Contact`
                   &emailTpl=`pvcVolunteerEmail`
                   &emailTo=`[[+what:WhatToWho]]`
                   &redirectTo=`171`
                   &validate=`who:required,
                              email:email:required,
                              what:required,
                              when:required,
                              where:stripTags,
                              how:stripTags,
                              why:stripTags`
        ]]
          • 3749
          • 24,544 Posts
          Ah, that almost certainly has to do with the processing order. I think at the time the modifier executes, the +what placeholder is not set.

          Try just putting it as a snippet on the page above the FormIt tag and, in the snippet, set a +ToWhom placeholder with:

          $modx->setPlaceholder('ToWhom', 'email_addresses_here');


          Then in the FormIt tag, just use this:

          &email=`[[+ToWhom]]`
          [ed. note: BobRay last edited this post 14 years, 11 months ago.]
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 6180
            • 31 Posts
            That certainly got me further along.

            Using some debugging I've been able to capture what is being sent into the snippet. This reveals my problem now: I am evidently passing to the snippet the name of the placeholder, not the value of it.

            So, here's how I'm calling the snippet, and the modifications I made to the populate the ToWhom placeholder. How do I get the value of the [[+what]] placeholder into the snippet?

            FormIt call:
            [[$pvcVolunteerForm]]
            [[!WhatToWho? &input=[[+what]] ]]
            [[!FormIt? &hooks=`email,redirect`
                       &emailSubject=`Volunteer Contact`
                       &emailTpl=`pvcVolunteerEmail`
                       &redirectTo=`171`
                       &validate=`who:required,
                                  email:email:required,
                                  what:required,
                                  when:required,
                                  where:stripTags,
                                  how:stripTags,
                                  why:stripTags`
                       &emailTo=`[[+ToWhom]]`
            ]]


            WhatToWho:
            <?php
            $modx->log(modX::LOG_LEVEL_DEBUG, $input); 
            if ($input == '') { return ''; }
              $flags = explode(',',$input);
              foreach ($flags as $key => $value) {
                switch ($value)
                {
                  case "ao":
                    $output[] = '[email protected]';
                    break;
                  case "ss":
                    $output[] = '[email protected]';
                    break;
                  case "sa":
                    $output[] = '[email protected]';
                    break;
                  default:
                    $output[] = '[email protected]';
                }
              }
              $modx->log(modX::LOG_LEVEL_DEBUG, implode(', ', array_unique($output))); 
              $modx->setPlaceHolder('ToWhom', implode(', ', array_unique($output)));


            The output to the error log is this:
            [2011-10-21 13:01:29] (DEBUG @ /index.php) [[+what]]
            [2011-10-21 13:01:29] (DEBUG @ /index.php) [email protected]
              • 3749
              • 24,544 Posts
              You shouldn't need to send anything to the snippet. It should use the values set in the form, which should be in the $_POST array(assuming that the form's method is "post").
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 6180
                • 31 Posts
                Bob, you almost lost me there. Thank you for mentioning the $_POST array, that clued me in to the change I needed to make in the snippet. I've been able to successfully test the process! Thank you!

                Here's the new look WhatToWho snippet:
                <?php
                $flags = $_POST['what'];
                
                if ($flags == '') { return ''; }
                  foreach ($flags as $key => $value) {
                    switch ($value)
                    {
                      case "ao":
                        $output[] = '[email protected]';
                        break;
                      case "ss":
                        $output[] = '[email protected]';
                        break;
                      case "sa":
                        $output[] = '[email protected]';
                        break;
                      default:
                        $output[] = '[email protected]';
                    }
                  }
                  $modx->setPlaceHolder('ToWhom', implode(', ', array_unique($output)));
                  • 3749
                  • 24,544 Posts
                  I'm glad you got it working. Thanks for reporting back. smiley
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting