We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36760
    • 136 Posts
    I'm working with FormIt 2.2.4 on Modx 2.2.14-pl and I need to have a select list where a visitor picks a Location, then based on that location the form information is sent to the specified addresses.

    I have a select list working where each option sends to a single email address:

    <select name="location" value="[[!+fi.location]]" id="location" class="input_box" style="width:272px;">
        <option value="">Select location...</option>
        <option value="[email protected]" [[!+fi.location:is=`[email protected]`:then=`selected`]]>Location 1</option>
        <option value="[email protected]" [[!+fi.location:is=`[email protected]`:then=`selected`]]>Location 2</option>
        <option value="[email protected]" [[!+fi.location:is=`[email protected]`:then=`selected`]]>Location 3</option>
    </select>


    With my emailTo being: &emailTo=`[[+location]]`

    The problem is, if I add more comma-separated addresses to each of the locations, I get this error: An error occurred while trying to send the email. You must provide at least one recipient email address.

    In addition to the select list, I've tried adapting the snippet from this topic: http://forums.modx.com/thread/92975/getting-formit-to-send-to-different-email-addresses-based-on-drop-down-selection to fit my needs. The snippet is then called before the email hook like in the thread.

    <?php
    $location = $hook->getValue('location');
    
    if($location == "1") {
        $hook->setValue('emailToAddresses', '[email protected],[email protected]');
    } elseif($location == '2') {
        $modx->log(xPDO::LOG_LEVEL_ERROR, 'Location 2 selected.');
    } elseif($location == '3') {
        $modx->log(xPDO::LOG_LEVEL_ERROR, 'Location 3 selected.');
    } else {
        $modx->log(xPDO::LOG_LEVEL_ERROR, 'No valid selection.');
    }
    
    return true;


    If I select location 2 or 3, the message is posted to the Modx Log, but if I pick location 1 I get the same error message as I mentioned above.

    Finally, to rule out an overarching issue with my FormIt, I changed my emailTo to be: &emailTo=`[email protected],[email protected],[email protected]`. This sends the form information to all of the addresses just fine.

    I appreciate any help!
      • 19872
      • 1,078 Posts
      Did you resolve this issue?

      Maybe the mixing an matching doesn't work as intended. The formIt placeholder perhaps cannot grab one email from one selected in the hook, but then go back to the form select for the location 2 and location 3.

      Does the hook work? Or are you being mislead to this that it works because location 2 and location 3 seem to function fine. Maybe formIt is getting the +location from the select menu and not the hook?

      <select name="location" ">
      <option value="0">Please choose...</option>
      <option value="1">Location 1</option>
      <option value="2">Location 2</option>
      <option value="3">Location 3</option>
      </select>


      $group[0] = '[email protected]'; // Default group (no choice)
      $group[1] = '[email protected],[email protected]'; // Location 1
      $group[2] = '[email protected]'; // Location 2
      $group[3] = '[email protected]'; // Location 3
      
      $userChoice = isset($_POST['location']) ? (int) $_POST['location'] : 0;
      return isset($group[$userChoice]) ? $group[$userChoice] : $group[0];


      Seems like the above would allow the user to safely send the form without actually selecting a location, since there is a default email to send to.

      Otherwise to force a required selected, maybe the following might work?

          <option value="">Select location...</option>
          <option value="1" [[!+fi.location:is=`1`:then=`selected`]]>Location 1</option>
          <option value="2" [[!+fi.location:is=`2`:then=`selected`]]>Location 2</option>
          <option value="3" [[!+fi.location:is=`3`:then=`selected`]]>Location 3</option>


      $group[1] = '[email protected],[email protected]'; // Location 1
      $group[2] = '[email protected]'; // Location 2
      $group[3] = '[email protected]'; // Location 3
      
      $userChoice = isset($_POST['location']) ? (int) $_POST['location'] : 0;
      return isset($group[$userChoice]) ? $group[$userChoice] : $group[0];
        • 4385
        • 372 Posts
        This might be late to the game, but I made something similar. I created a snippet/hook to override the default email address to another email based menu selecting the subject of the inquiry. The only tricky part is the snippet is hardcoded.


            	<select class="form-control" name="Subject" value="[[!+fi.Subject]]">
        	<option value="">Select an Subject...</option>
                <option value="ACCOUNT INFORMATION" [[!+fi.Subject:is=`ACCOUNT INFORMATION:then=`selected`]]>Account Information</option>
                <option value="ATM" [[!+fi.Subject:is=`ATM:then=`selected`]]>ATMs</option>
                <option value="BUSINESS ACCOUNTS" [[!+fi.Subject:is=`BUSINESS ACCOUNTS:then=`selected`]]>Business Accounts</option>
                <option value="BUSINESS LOANS" [[!+fi.Subject:is=`BUSINESS LOANS:then=`selected`]]>Business Loans</option>
                <option value="CHECK ORDER" [[!+fi.Subject:is=`CHECK ORDER:then=`selected`]]>Check Orders</option>
                <option value="CREDIT CARD" [[!+fi.Subject:is=`CREDIT CARD:then=`selected`]]>Credit Cards</option>
        ...
        



        <?php
        $emailRouter = $hook->getValue('Subject');
         
        switch ($emailRouter) {
            case "CREDIT CARD":
                $hook->setValue('emailAddresses', '[email protected]');
                break;
            case "BUSINESS ACCOUNTS":
                $hook->setValue('emailAddresses', '[email protected]');
                break;
            case "BUSINESS LOANS":
                $hook->setValue('emailAddresses', '[email protected],[email protected]');
                break;
            default:
            $hook->setValue('emailAddresses', '[email protected]');
        }
        
        return true;
        

          DropboxUploader -- Upload files to a Dropbox account.
          DIG -- Dynamic Image Generator
          gus -- Google URL Shortener
          makeQR -- Uses google chart api to make QR codes.
          MODxTweeter -- Update your twitter status on publish.
          • 50050
          • 10 Posts
          hey,
          a bit late but... maybe somebody else will find it useful.
          ive achieved exactly what you wanted without any extra snippets.

          [[!FormIt?
          &hooks=`email,redirect`
          &emailTpl=`emailTpl`
          &emailTo=`[[+showrooms]]`
          &redirectTo=`22`
          ]]

          <select id="showrooms" name="showrooms" class="form-control required">
          <option disabled="disabled" selected value="">Please Select a Showroom</option>
          <option value="[email protected]">Jandakot</option>
          <option value="[email protected]">Joondalup</option>
          <option value="[email protected]">Osborne Park</option>
          <option value="[email protected]">Mandurah</option>
          </select>
            • 46718
            • 39 Posts
            I am still having an issue with getting this to work with FormIt 2.2.7.

            I attempted the solution from @bwente and it works fine when one email address is entered, but not when multiple are set.

            A simplified version of my code is here:
            [[!FormIt? 
              &hooks=`formItMultiEmail,email,redirect`
              &emailTpl=`myTpl`
              &emailTo=`[[+emailToAddress]]`
              &redirectTo=`57`
            ]]
            <form action="[[~[[*id]]]]" method="post">
            <select name="recip" class="form-control">
            	<option value="0" [[!+fi.recip:formitisselected=`0`]]>Option 1</option>
            	<option value="1" [[!+fi.recip:formitisselected=`1`]]>Option 2</option>
            </select>
            </form>
            


            The formItMultiEmail looked like this:
            $recip = $hook->getValue('recip');
            
              switch ($recip) {
                case "0":
                  $hook->setValue('emailToAddress', '[email protected]');
                  break;
                case "1":
                  $hook->setValue('emailToAddress', '[email protected],[email protected]');
                  break;
                default:
                $hook->setValue('emailToAddress', '[email protected]');
              }
            
              return true;
            


            With this code, Option 1 works properly and Option 2 fails with the error: "An error occurred while trying to send the email. You must provide at least one recipient email address.".

            Has anyone been able to resolve this?

            Also, the reply from @Daniel works because there are not multiple email addresses in his example.