I have a site where users can select interests when they register. They can select more than one, but I tried in vain to find an example of using checkboxes to toggle an extended field container with multiple possible values (e.g. favouritefoods = cheese AND wine AND chocolate), so I created my own. Thought it might be useful to somebody.
Please note: I’ve used this in conjunction with the Login Profile snippet - it uses the Login Profileupdate submit button. Use the
[[!Profile? &useExtended=`1`]] [[!UpdateProfile]]
calls in the page.
//checkbox_extended
//Snippet to generate checkboxes that add/remove extended field values in a user's profile. This is used in conjunction with the Login Profile snippet.
<?php
//get your checkbox values - defined in your snippet call as &boxes=`checkbox1,checkbox2,checkbox3...`
$checkboxvalues = explode(",",$boxes,99);
//get extended field values from the user profile - use &container=`` in your snippet call to define the container name of the extended value(s). You'll find this in Manager > Security > Manage Users
$profile = $modx->user->getOne('Profile');
$extended = $profile->get('extended');
$extendedvalues = $extended[$container];
//compare the two and identify any matches. e.g. if checkbox1 == extendedfieldvalue1 then return a 'checked' result
for($x = 0; $x < count($checkboxvalues); $x++)
{
if(in_array($checkboxvalues[$x],$extendedvalues))
{
$selected = 'checked';
}else{
$selected = '';
}
//output the checkboxes - you can style it how you want here
$output.= '<div class="option"><input name="'.$container.'[]" number="" type="checkbox" value="'.$checkboxvalues[$x].'"'.$selected.'> '.$checkboxvalues[$x].'</input></div> ';
}
return $output;
Then in the page, use:
[[!checkbox_extended? &container=`yourextendedfieldcontainername` &boxes=`checkbox1,checkbox2,checkbox3...etc`]]
This works nicely for one container, I haven’t tried using it for multiple containers (e.g. favouritefoods, favouritecars etc.) but i don’t see why you can’t use the snippet more than once on the same page.