General developer forum

Custom Profile Fields - Multiple Checkboxes.

Picture of Evan Glicakis
Re: Custom Profile Fields - Multiple Checkboxes.

Alright so I figured out how to do it 

in the edit_field_add function you would create your checkbox elements, i use the var $i to give the array key's values however, it can be 'name[]' 

options is an array of strings containing the list of checkbox options. I use these as my values.

$i = 1;
$checkboxes = array();
foreach ($this->options as $key) {
     $checkboxes[] = &$mform->createElement('advcheckbox', $this->inputname . "[$i]", '', $key, array('group' => 1), array('',$key));

to retrieve the values of this form simply call $_POST['name] and it will return either the $key or '' depending if the box is ticked or not. 

To save these values to the database, I iterate through the values that were ticked an store them. With the edit_save_data_preprocess method. I'm not too sure if this is the preferred way of doing it, but it works.

Average of ratings: -