Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm using a select list in my custom module. When I select some option in the list after submitting the form, I can see the $form_state['values']['select_field'] is showing an integer.

How can I make the form_state values to show the title of the list, instead of number of the selected option?

share|improve this question
    
@inizio Code blocks are not for highlighting random words. –  kiamlaluno Feb 19 at 6:35
    
OK got it. Next time ill take care of it :) –  inizio Feb 19 at 6:39
add comment

1 Answer

up vote 3 down vote accepted

Whatever you set as the array keys for options will be the values, so if you want to see option content rather than value, you need to set it up like that:

$form['select_field'] = array(
  '#options' => array('Text' => 'Text', 'More Text' => 'More Text'),
);

Or, leave the numeric keys where they are, and refer back to the original options array in your submit handler:

$selected_index = $form_state['values']['select_field'];
$display_value = $form['select_field']['#options'][$selected_index];

If you go with the first option, drupal_map_assoc() will probably be useful.

share|improve this answer
    
+1 for drupal_map_assoc() –  Wtower Feb 18 at 13:38
    
I'm getting the result of select list from my database, so the second way did great job. Thank you very much. –  user2519032 Feb 18 at 13:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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