1

I am using Form api to build one form.In that I have used select type field and for that options are coming from an array. Below is the code

  $fnames = array();
  $dir_path = variable_get('file_public_path', conf_path(). '/files');
  $files1 = scandir($dir_path); 
  $dirs = glob($dir_path . '/*' , GLOB_ONLYDIR);
    foreach($dirs as $directs){
     $names = explode('/', $directs);//
     $fnames[] = end($names);
    }

  $form['select_list'] = array(
    '#type' => 'select',
    '#title' => t('Folder Names'),
    '#options' => $fnames,
  );

While displaying on the select list it will display all names but when I will fetch the value in one of the submit function it will show only index value not the name.

function custom_submit_file_upload($form, &$form_state){
  $foldername = $form_state['values']['select_list'];
}

Here it shows,

$foldername = 0 or 1 or 2 whatever I select in the options, 

but I need name of the selected element not the index values. Please suggest me to get the names.Thank you in advance..

1 Answer 1

2

Since the form submission handler has access to the $form variable, you just use the following code.

$foldername = $form['select_list']['#options'][$form_state['values']['select_list']];

To make it clear with an example: $form['select_list']['#options'] is an array like array( 1 => 'first folder', 2 => 'second folder'), and $form_state['values']['select_list'] is a value between 1 and 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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