Forum for discussing general topics related to Couch.
11 posts Page 1 of 2
I'm trying to build a contact form with a dropdown list of recipients. Ideally this list, both names and email addresses,should be editable in the admin panel. I see all the tags, such as cms:cloak_email and cms:input type='dropdown', but I can't see how to put them all together.

cms:input needs it's option list as strided list, but HTML doesn't support concatenation, so it's hard to see how to build that from cms:cloak_email. And I can't quite see how I could make the email addresses and names admin-editable.

Any pointers would be welcome.
TOF.
Hi TOF,

So, say the dropdown shows multiple recipients to choose from - will the visitor choose just one of them? Or can multiple recipients be chosen to send the mail to?

I ask this because Couch does not support multi-select dropdowns (yet).
If multiple selections are to be made, we'll have to use checkboxes instead.

Could you please confirm?
Thanks.
Only one needs to be selected and the mail sent to just the one selected. This is for church website, the list will be the clergy, which includes me as webmaster. I could do it with a PHPmailer on the host, but that makes editing the list a server-access thing. And thanks for your help.

TOF
OK. Here is what we can do -
Suppose we had three hard-coded emails to show, following is how the cms:input dropdown could be defined -
<cms:input
name='email_to'
label='To:'
opt_values='John Doe=john@doe.com | Jane Doe=jane@doe.com | Jack Sparrow=jack@sparrow.com'
type='dropdown'
/>

The 'John Doe=john@doe.com | Jane Doe=jane@doe.com | Jack Sparrow=jack@sparrow.com' string above is what creates the three options in the dropdown.

Our aim is to dynamically generate this string using values inputted in the admin-panel.
To do that, let us use a repeatable-region to input the emails.
Define it as follows (in your contacts template itself would be perfect) -
Code: Select all
<cms:template title='Contacts'>

    <cms:repeatable name='recipients' label='Mail recipient' >
        <cms:editable name='name' label='Name' type='text'  />
        <cms:editable name='email' label='EMail' type='text' validator='email' />
    </cms:repeatable>
   
</cms:template>

You can now input the names and email addresses of as many recipients as you like.
mail.jpg
mail.jpg (12.96 KiB) Viewed 2254 times

Now modify the original definition of the cms:input in the form to follows -
Code: Select all
<cms:capture into='my_recipients_list'>
    <cms:show_repeatable 'recipients'>
        <cms:if name && email>
            <cms:show name />=<cms:show email /> |
        </cms:if>
    </cms:show_repeatable>
</cms:capture>

<cms:input
    name='email_to'
    label='To:'
    opt_values="<cms:show my_recipients_list />"
    type='dropdown'
/>

As you can see, just above the cms:input we use a cms:capture block that loops through the repeatable-regions, crafts the 'John Doe=john@doe.com | Jane Doe=jane@doe.com | Jack Sparrow=jack@sparrow.com' like string and sets it in a variable named 'my_recipients_list'.

We then use that variable in cms:input.

Hope this helps. Do let us know.
Wonderful, it works well. Thanks very much. Thanks particularly for the speed.
The method you used has opened up all sorts of other possibilities. I have another site that I have always managed statically that can benefit from this approach, not to the contact page, but to complex attribute searches.

TOF
You are welcome :)
I'm glad you found the solution useful.
I would like to suggest a refinement to the method above. The code as written seems to expose the email in plain text on the page.
Code: Select all
<cms:if name && email>
    <cms:show name />=<cms:show email /> |
</cms:if>
       
RENDERS AS

<select name="email_to" id="email_to" >
   <option value="john@doe.com">John Doe</option>
   <option value="jane@doe.com">Jane Doe</option>
   <option value="jack@sparrow.com">Jack Sparrow</option>
</select>

I think what you want instead is to craft the form itself showing just the name, then insert the email address later while processing the form.
Code: Select all
<cms:if name && email>
    <cms:show name /> |
</cms:if>

Then within the k_success routine, swap in the email address.
Code: Select all
<cms:show_repeatable 'recipients'>
   <cms:if frm_name == name><cms:set recipient = email  'global' /></cms:if>
</cms:show_repeatable>

Finally, you would use <cms:show recipient /> as the "to" value in the <cms:send_mail> routine.
I agree @tim - your approach is better in protecting the addresses from being exposed.
I was about to query if it was possible to obscure the email addresses. So thank you for this Tim.

However, I cannot put this:
Code: Select all
<cms:send_mail
             from=k_email_from
               to=<cms:show recipient />
          subject='Feedback from xxxx'>
          The following message came from a visitor to xxxx:
            <cms:show k_success />
</cms:send_mail>


as it gives at PHP error:
Code: Select all
ERROR! ATTRIB_VALUE: Invalid first char "<" 


Any ideas as to how to do this better? (My PHP is minimal)
TOF.
The value being provided to the 'to' parameter either needs to be enclosed within double-quotes as follows -
Code: Select all
to="<cms:show recipient />"

or you can simply put the variable's name there -
Code: Select all
to= recipient

Please see the following for a discussion on the right way to set parameters -
http://docs.couchcms.com/concepts/setti ... eters.html

Hope it helps.
11 posts Page 1 of 2