Skip to main content
Inspiring
April 23, 2009
Question

Defining form recipients by CFSWITCH?

  • April 23, 2009
  • 3 replies
  • 832 views

I am working with a form that has several check boxes on it. All of the check boxes have the same name so when the form processes I get a response like, check1,check2,check3 etc...

I want to set this up so that depending on what box(es) are checked different e-mail addresses will receive the form data.

I had tried using CFIF statements like with each checkbox individually named.

<cfif #FORM.check01# is "one">

<cfset emailRecip01="one@address.com">

</cfif>


<cfif #FORM.check02# is "two">

<cfset emailRecip02="two@address.com">

</cfif>


<cfif #FORM.check03# is "three">

<cfset emailRecip03="three@address.com">

</cfif>


<cfmail to="#emailRecip01# #emailRecip02# #emailRecip03#"

subject="Form Response"

from="server@address.com"

type="html">

This works fine if there is one address per checkbox, but I have two addresses and 11 check boxes.

Because of this I tried to use a CFSWITCH command and setup the following commands (shortened for this post).

<cfswitch expression="#FORM.checkBox#">

<cfcase value="one;two" delimiterters=";">

    <cfset eAddress1="one@address.com">

    </cfcase>

    <cfcase value="three" delimiters=";">

    <cfset eAddress2="two@address.com">

    </cfcase>

    <cfdefaultcase>

    <cfset eAddress1="three@address.com">

    <cfset eAddress2="">

    </cfdefaultcase>

</cfswitch>


<cfmail to="#eAddress1# #eAddress2#"

subject="Form Response"

from="server@address.com"

type="html">

I am only receiving e-mails at the <cfdefaultcase> address. Why is this not working?

Thanks,
Jeremiah

    This topic has been closed for replies.

    3 replies

    JTrueAuthor
    Inspiring
    April 27, 2009

    Hi, I actually was able to find a solution based on both your suggestions. Instead of using listfind, I used list length and separated the check boxes into two groups, checkbox1 and checkbox2. Then I used CFSET to check the length of the string returned, delimited by commas. From this I was able to write conditional statements, code below.

    <cfset listLength01 = ListLen("#FORM.checkBox1#", ",")>

    <cfset listLength02 = ListLen("#FORM.checkBOx2#", ",")>



    <cfif #listLength01# GREATER THAN "0">

    <cfset emailRecip01="address1@server.com">

    </cfif>


    <cfif #listLength02# GREATER THAN "0">

    <cfset emailRecip02="address2@server.com">

    </cfif>


    <cfif #listLength01# is "0" and #listLength02 is "0">

    <cfset emailRecip01="address3@server.com">

    </cfif>

    <cfmail to="#emailRecip01#,#emailRecip02#"

    subject="Form response"

    from="cfserver@server.com"

    type="html">

    Thanks to both of you both of your help!

    Jeremiah

    ilssac
    Inspiring
    April 23, 2009

    As mentioned above <cfswitch...> is probably not what you want here.  <cfswitch...> is not a loop it does not check multiple values.  It checks a single value agains multple choices.  All it really is short hand for an if|elseif|else block where the same expression is compared against different values for every branch.

    You will need logic or functions that compares each value in the form list.

    Inspiring
    April 23, 2009

    If sample values are check1,check2,check3,

    and your cfcase is looking for values like:

    <cfcase value="one;two" delimiterters=";">

    You will never get a match.

    Switch/case is not going to give you what you want anyway.  You'll need to use listfind() or something similar.