carolineschnapp
7/25/2013 - 4:11 PM

Example where customer tags are collected in the REGISTRATION form, and more than one tag. The server expects a comma-separated list of tag

Example where customer tags are collected in the REGISTRATION form, and more than one tag. The server expects a comma-separated list of tags. So we give that list to him via a hidden input field that we update with JavaScript.

In the SIGNUP form, it'd be the same code except you replace customer[tags] with contact[tags].

<p>I am interested in:</p>
<p>
  <input type="checkbox" class="interested-in" value="sunsets">Sunsets<br />
  <input type="checkbox" class="interested-in" value="piña coladas">Piña coladas<br />
  <input type="checkbox" class="interested-in" value="getting lost in the rain">Getting lost in the rain<br />
  <input type="checkbox" class="interested-in" value="songs by rupert holmes">Songs by Rupert Holmes<br />
</p>

<!-- this is what is submitted to the server -->
<input type="hidden" name="customer[tags]" />

<script>
jQuery(function() {
  jQuery('.interested-in').change(function() {
    var tags = '';
    jQuery('.interested-in:checked').each(function(){
      tags += jQuery(this).val() + ',';
    });  
    jQuery('[name*="tags"]').val(tags);
  });
});
</script>