1

I have a select field for a content type which has options generated dynamically. The options are updated in hook_form_alter(). The problem is that when I try to manipulate the posted data in submit callback, the field values are empty. Below is an example to better understand the issue.

function my_form_alter(&$form, &$form_state) {
    $options = array('a' => 1, 'b' => 2, 'c' => 3);
    $form['my_field'] = array(
        '#type' => 'select',
        '#title' => t('Label'),
        '#multiple' => TRUE,
        '#options' => $options,
        '#default_value' => '',
    );
    $form['#submit'][] = 'my_submit_handler';
}

function my_submit_handler($form) {
    echo "<pre>";
    var_dump($form['my_field']);
    echo "</pre>";
}

If I select options 2 and 3 for example; the var_dump returns empty array().

1

Submitted values are contained in $form_state['values']:

function my_submit_handler($form) {
  $selected_options = array_filter($form_state['values']['my_field']);
}

e.g. if you select '1' and '2' on the form, $selected_options will be array('a', 'b')

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.