Add Contacts to Groups via Form Submissions

This tutorial will show you how to add a contact to a group as part of a form submission.

To add a contact to a group as part of a form submission, use the form attribute contactgroupadd="/insert-group-id-here/":

<forms:form id="lead_capture" contactgroupadd="/insert-group-id-here/" recordsubmissions="true" onsubmitredirect="/success/" contactmode="store"> <forms:row label="First and Last Names"> <forms:editbox id="contact_first_name" width="80" validations="mandatory" /> <forms:editbox id="contact_last_name" width="110" validations="mandatory" /> </forms:row> <forms:row label="E-mail Address"> <forms:editbox id="contact_email" width="200" validations="mandatory,email" transformations="tolower" /> </forms:row> <forms:row type="one_column" align="center"> <forms:submitbutton value="Submit" /> </forms:row> </forms:form>


You will need to specify the ID of the group you'd like contacts added to. To find the group ID, go to Dashboard > Customers > Groups > Find the Group, click "Edit Properties".

Combobox to Select a Group

You can further customize this solution to allow the user to select a group from a combobox.

This approach is commonly used to help sort contacts into groups based on why they enquired.

The Combobox dropdown will populate with groups dynamically (eg. populate with all child groups of the group with id = '/what-can-we-help-with/').


To get started, setup a heirachy of groups via Dashboard > Customers > Groups:


Add a combobox to the form, where the options for the combobox populate dynamically with groups:

<forms:row label="What can we help you with?"> <forms:combobox title="What Can We Help You With?" id="form_reason" datatitle="$group['group_title']" as="group" datavalue="$group['group_id']" datasource="\Components\Customers\Groups::getChildren('/what-can-we-help-with/')" validations="mandatory" /> </forms:row>


You will also need to add some additional attributes to the form:

contactgroupadd="$form_reason.value"
Instead of defining a group ID, update the contactGroupAdd attribute, so that it uses the value of the combobox with ID=form_reason.

Here is the final solution: 

<forms:form id="lead_capture" contactgroupadd="[? $form_reason.value ?]" recordsubmissions="true" onsubmitredirect="/success/" contactmode="store"> <forms:row label="First and Last Names"> <forms:editbox id="contact_first_name" width="80" validations="mandatory" /> <forms:editbox id="contact_last_name" width="110" validations="mandatory" /> </forms:row> <forms:row label="E-mail Address"> <forms:editbox id="contact_email" width="200" validations="mandatory,email" transformations="tolower" /> </forms:row> <forms:row label="What can we help you with?"> <forms:combobox title="What Can We Help You With?" id="form_reason" datatitle="$group['group_title']" as="group" datavalue="$group['group_id']" datasource="\Components\Customers\Groups::getChildren('/what-can-we-help-with/')" validations="mandatory" /> </forms:row> <forms:row type="one_column" align="center"> <forms:submitbutton value="Submit" /> </forms:row> </forms:form>

Checkboxes to Add to Multiple Groups

Taking the above example a step further, you can allow users to select multiple groups by employing checkboxes instead of a drop-down combobox:

<forms:form id="lead_capture" contactgroupadd="[? implode(',' , $lead_capture.getValueForFormItem('form_reason')) ?]" recordsubmissions="true" onsubmitredirect="/success/" contactmode="store"> <forms:row label="First and Last Names"> <forms:editbox id="contact_first_name" width="80" validations="mandatory" /> <forms:editbox id="contact_last_name" width="110" validations="mandatory" /> </forms:row> <forms:row label="E-mail Address"> <forms:editbox id="contact_email" width="200" validations="mandatory,email" transformations="tolower" /> </forms:row> <forms:row label="What can we help you with?"> <data:repeater as="group" datasource="\Components\Customers\Groups::getChildren('/what-can-we-help-with/')"> <forms:checkbox id="[? 'form_reason_' + $group['group_id'] ?]" valuechecked="[? $group['group_id'] ?]" name="form_reason[]" label="[? $group['group_title'] ?]" /><br /> </data:repeater> </forms:row> <forms:row type="one_column" align="center"> <forms:submitbutton value="Submit" /> </forms:row> </forms:form>


To explain
:

The value of the checkboxes are grouped together in an array, which can be referenced by the name="" attribute used on the comboboxes. 

You can get the value of the array of comboboxes by using this function:

$lead_capture.getValueForFormItem('form_reason') = [];

Where $lead_capture is the form ID, and 'form_reason' is the name="" attribute of the comboboxes.

The contactgroupadd="" attribute of the form cannot accept an array, it needs group ID's separated by commas in a format like this: 

contactgroupadd="/group-one-id/,/group-two-id/,/group-three-id/"

To achieve this, the PHP implode function is used to separate the group ID's by a comma character:

contactgroupadd="implode(',' , $lead_capture.getValueForFormItem('form_reason'))"