I would like to add a 'Send message to' dropdown menu to my contact form. The options would be 'Service' and 'Sales'. They would each point to a pre-defined email address and determine the send_mail tag's 'to' parameter. So far I have been unable to come up with a solution through couch.
The submitted values of all the elements in your form are available for use after successful form submission.
So, suppose the dropdown element you mentioned is named 'send_to', the value selected within it would be available as a variable named 'frm_send_to' (the string 'frm_' gets prefixed to the names of the input elements).
You can easily use the submitted value as a parameter for the 'send_mail' tag.
Following is a working example:
- Code: Select all
<cms:form method="post">
<cms:if k_success >
<h3>Thanks for your submission. We'll get back to you.</h3>
<cms:if frm_send_to='Service'>
<cms:set my_mail_address='service@mydomain.com' />
<cms:else/>
<cms:set my_mail_address='sales@mydomain.com' />
</cms:if>
<cms:send_mail from=frm_email to=my_mail_address subject='Feedback from your site' >
The following is an email sent by a visitor to your site:
<cms:show k_success />
</cms:send_mail>
</cms:if>
<cms:if k_error >
<h3>Failed to submit form</h3>
<cms:each k_error >
<cms:show item /><br>
</cms:each>
</cms:if>
Your Email: <cms:input type="text" size="10" name="email" required='1' validator='email' /><br />
Your message: <cms:input type="textarea" cols="35" rows="5" name="message" required='1' /><br />
Send message to: <cms:input type="dropdown"
name="send_to"
opt_values="Service | Sales"
/>
<cms:input name="submit" type="submit" value="Send" />
</cms:form>
V. IMP. Notice that we have not coded the email addresses as values of the dropdown list. Instead we check the submission and supply a hardcoded email address ourselves.
- Code: Select all
<cms:if frm_send_to='Service'>
<cms:set my_mail_address='service@mydomain.com' />
<cms:else/>
<cms:set my_mail_address='sales@mydomain.com' />
</cms:if>
This is to prevent the possibility of the form being misused by spammers who could otherwise fabricate post submissions and send emails to arbitrary addresses.
Do let me know if this helped. Thanks.