by
KK » Mon Mar 10, 2014 3:46 pm
Hi Tom,
Yes, I got it. Thanks.
There are two distinct sections in your form -
a. A list of checkboxes showing available items for sale
b. The regular inputs of contact forms e.g. name. email etc.
The second part mentioned above is straightforward.
The first part, however, will require some thinking as we want the checkboxes to be dynamically populated from a repeatable region.
I'll explain one way of doing it (I'll post the full sample code at the end).
Please create a Couch template (say named 'orders.php') and define a repeatable region with a single textbox like this
- Code: Select all
<cms:repeatable name="items" label="Autumn crops">
<cms:editable name="desc" label="Description" type="text"/>
</cms:repeatable>
Register the template and fill out the repeatable regions with some sample data -

- Untitled-1.png (26.01 KiB) Viewed 3477 times
Now on the template, we'll create checkboxes for each of the items in the repeatable region.
If you had a look at our docs, you'll remember that the cms:input type='checkbox' tag accepts a parameter named 'opt_values' that makes it create multiple checkboxes e.g. the following code
<cms:input name='test' type='checkbox' opt_values='one || two || three' />
will create three checkboxes (the '||' places each checkbox on a separate line).
So, if we can create the '||' separated string where each part is a product that we have, we can easily create the checkboxes.
The following code does just that
- Code: Select all
<cms:show_repeatable 'items' >
<cms:set my_opt_values="<cms:concat my_opt_values '||' desc />" scope='global'/>
</cms:show_repeatable>
The code above loops through our repeatable region and concats all the values in a variable named 'my_opt_values'.
Next we use the string we created above to create the checkboxes
- Code: Select all
<cms:input type="checkbox" name="crops" label="Selected crops" opt_values=my_opt_values />
You should now see all products listed out as checkboxed items.
I am attaching the full sample code below.
Test it out as it to get a feel of how it works.
You'll need to use your own CSS to make it look good.
- Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title='Orders'>
<cms:repeatable name="items" label="Autumn crops">
<cms:editable name="desc" label="Description" type="text"/>
</cms:repeatable>
</cms:template>
<cms:form anchor='0' method="post">
<cms:if k_success>
<cms:show k_success />
</cms:if>
<cms:if k_error>
<cms:show k_error />
</cms:if>
<h1>Autumn Crops: </h1>
<cms:show_repeatable 'items' >
<cms:set my_opt_values="<cms:concat my_opt_values '||' desc />" scope='global'/>
</cms:show_repeatable>
<cms:input type="checkbox" name="crops" label="Selected crops" opt_values=my_opt_values /><br/>
<h1>Order:</h1>
Full Name: <br />
<cms:input name='name' label='Name' type="text" /> <br />
Email: <br />
<cms:input name='email' label='Email' type="text" validator='email' required='1' /> <br />
Message: <br />
<cms:input type="text" name='message' label='Message' class="name-email" placeholder="Extra Quantities" /> <br />
<input type="submit" value="Submit"/>
</cms:form>
<?php COUCH::invoke(); ?>
When the form gets submitted, all the selected values are made available to us.
We can then take action using the submitted value e.g. email them etc.
In the sample form above, we are simply displaying the submitted values.
To email them, you can use the cms:send_mail tag e.g.
- Code: Select all
<cms:if k_success >
<p>Thank you. We'll get back to you as soon as possible.</p>
<cms:send_mail from=k_email_from to=k_email_to subject='Order form submission'>
The following is an email sent by a visitor to your site:
<cms:show k_success />
</cms:send_mail>
</cms:if>
If you are not sure how forms work in Couch, i suggest you please see the following pages from our docs:
http://www.couchcms.com/docs/concepts/forms.htmlhttp://www.couchcms.com/docs/tutorials/ ... -form.htmlhttp://www.couchcms.com/docs/tags-refer ... _mail.htmlHope this helps.