简体   繁体   中英

How to send email based on CFM form selected value

I have a javascript form that links and works perfectly to a .cfm form that sends an email to the correct people. I want to send an email to one person if option A is selected and to another person if option B is selected.

Do I do this with Javascript? If so how do i connect it to the .cfm email form?

something like:

if (option == 'a')

send email to john

else if (option == 'b')

send email to tom

What is the syntax to send the email? Should I be doing this with coldfusion syntax instead?

In your .cfm file, it's as simple as:

<cfif form.option EQ "a">
  <cfset mailto="john@example.com">
<cfelseif form.option EQ "b">
  <cfset mailto="tom@example.com">
<cfelse>
<!--- you should have a default if option could be non-selected --->
  <cfset mailto="jane@example.com">
</cfif>

<cfmail to="#mailto#" ...>

OR if the user can only select from A or B, then you don't need the else-if part of this, and it can be simplified to:

<cfif form.option EQ "a">
  <cfset mailto="john@example.com">
<cfelse>
  <cfset mailto="tom@example.com">
</cfif>

<cfmail to="#mailto#" ...>

Also, one thing to be aware of... if you are using a checkbox or radio button form element, HTML will NOT submit any value for that form element if the user doesn't select any value.

So I like to write my code like this:

<cfset option = "default">  <!--- setup whatever default value you want here --->
<cfif isDefined( "form.option" )>
   <cfset option = form.option>
</cfif>

<cfif option EQ "a">
   <cfset mailto="john@example.com">
<cfelseif option EQ "b">
   <cfset mailto="tom@example.com">
<cfelseif option EQ "default">
   <!--- you should have a default if option could be non-selected --->
  <cfset mailto="jane@example.com">
</cfif>

<cfmail to="#mailto#" ...>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM