Imagine this scenario. You create an add form with checkboxes among other fields. When the user submits, your form stores this information in a database table. Then the user decides to edit his selection. He goes to the edit form. There he sees his previous choices already selected. This modifies his selection and his modifications are registered on a database table.
This simple scenario is quite complicated to implement in drupal. When faced with this problem, I rigorously searched through the drupal documentation and blogs to find the solution. Unfortunately, I couldn't find anything. Then I looked into the existing drupal core modules and imitated their functions. I wrote this page so that others don't have to spend as much time as I did on this problem.
Checkbox data is stored in an associative array. The format of the associative array is Array(id=>name, id2=>name2, ...) by default. The checkboxes are populated by the value of #options attribute. The #default_value attribute defines the default value(s). The default values are unchecked for each checkbox.
Once a form is processed, drupal merges all keys and values. To make an editable format, we would be exploiting this property. We will pass Array(id=>value,...) to #options and nothing to #default_value. In the edit form, we would pass Array(name=>name) to #options and Array(id=>name) to #default_value. When the form array is merged, all values with id=>name will replace their corresponding name=>name pairs.
Thus we end up with a list of all option displayed and chosen ones checked.
/**
* Implementation of hook_form().
*/
function projects_form(&$node) {
// project title
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Project Title'),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5
);
// project description
$form['body_filter']['body'] = array(
'#type' => 'textarea',
'#title' => t('Project Description'),
'#default_value' => $node->body,
'#rows' => 15,
'#required' => TRUE
);
$form['body_filter']['format'] = filter_form();
// users sharing the project
if (arg(1) == 'add') {
// returns Array(id=>name,id2=>name2 ...)
$checklist = addformlist();
} else {
// returns Array(name=>name,name2=>name2 ...)
$checklist = editformlist();
// returns Array(id=>name,id2=>name2 ...)
$shred = previouslyselected($node->nid);
}
// my checkboxes
$form['sharers'] = array(
'#type' => 'checkboxes',
'#title' => t('Share with'),
'#default_value' => $shred,
'#options' => $checklist,
'#description' => t('some description.'),
'#required' => FALSE
);
return $form;
}
addformlist() return the options in Array(id=>name) format. editformlist() return a options in Array(name=>name) format. previouslyselected() returns a list of previously selected checkboxes in Array(id=>name) format. The important variables are the $checklist and $shred arrays. If you understand the format they expect and how to apply them, then that's all there is to it.
Comments
checkboxes
after submit this form how can i get checkboxes value or checked values?
please help me