phpdrupaldrupal-7drupal-fapidrupal-forms

How do I save checkbox values from Custom module in Drupal?


How do I save the status of checkboxes using the hook_submit?

hook_form:

$versionArray = array('ver 1.7.0.0-beta1' => 'ver 1.7.0.0-beta1', 'ver 1.6.2.0' => 'ver 1.6.2.0', 'ver 1.5.1.0' => 'ver 1.5.1.0', 'ver 1.4.2.0' => 'ver 1.4.2.0', 'ver 1.3.3.0' => 'ver 1.3.3.0');

$form['mage']['version'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Select'),
    '#required' => TRUE,
    '#options' => $versionArray
);  

hook_submit:

....
$node->field_checkboxes[0]['value'] = $form_state['values']['mage']['version'];
....
node_save($node)

I have got all my other fields saving correctly, but these fields remain blank.

Thanks Robert


Solution

  • In your submit function try to use the following code

    $selected = array();
    foreach($form_state['values']['version'] as $a => $b)
    {
        if((string)$b != "0")
        {
             $selected[] = $b;
        }
    }
    variable_set("var_selected", $selected);
    

    Hope this helps... Muhammad.