Forum for discussing general topics related to Couch.
4 posts Page 1 of 1
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.
Hi,

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.
I was unaware that couch generated the frm_* variables. This is great to know, as it also gives me more flexibility with the format of the generated email.

I noticed an issue with the html output for the dropdown type input.

Code:
Code: Select all
<cms:input type='dropdown' name='send_to' opt_values="Service | Sales" opt_selected='Service' required='1' title="Send to" />

Output:
Code: Select all
<select name="send_to"id="send_to"  title="Send to"><option value="Service"  selected="selected">Service</option><option value="Sales">Sales</option></select>

The name and id parameters for the select tag have no spacing in between them. KK, please let me know if this output is specific to my patched (html-tag) couch setup. Nevertheless thank you for the assistance, the form is working just how I imaged it. :D
Hi,

I am glad it helped.

The way the form variables become available at submission is documented at http://www.couchcms.com/docs/concepts/forms.html (under heading ' 2. Accessing input fields individually').

Regarding the lack of space between the two parameters that you noticed - that happens to be present in the main trunk too. I have rectified it. Thank you for bringing it to my notice.

Regards
4 posts Page 1 of 1