I have a form field with type select. The results values are taken from the database. If the select list is with single value I'm displaying the value title on the submit like this:

   $selected_index = $form_state['values']['select_list']; //the field is select_list
   $display_value = form['select_list']['#options'][$selected_index];
   dpm($display_value); //it's showing the selected option value title.

When I'm using '#multiple' => TRUE; into the form field, It's showing error: Warning: Illegal offset type in...

How can I display the titles of the multiple values?


This is my code:

function mymodule_page($form, &$form_state) {

global $user; 

$result = db_query('SELECT n.nid, n.title, n.created
FROM {node} n WHERE n.uid = :uid and type = :type', array(':uid' => $user->uid, ':type' => 'outlets'));

$droplist = array();
foreach($result as $record) {
    $title = $record->title;
    array_push($droplist, $title); 

    $form['select_list'] = array(
       '#type' => 'select',
       '#title' => 'Select list',
       '#options' => $droplist, //I'm getting this list from my database
       '#multiple' => TRUE,   

function mymodule_submit($form, &$form_state) {
   $selected_index = $form_state['values']['select_list'];

   foreach($selected_index as $key => $sindex)
  • please dpm($form_state['values']['select_list']) in submit and get snap shot of nested row. tnx
    – Yuseferi
    Feb 19 '14 at 11:42
  • Here's a screenshot imageshack.com/a/img827/3339/4vea.jpg Feb 19 '14 at 11:48
  • This is when a select the second and third options in the list. Feb 19 '14 at 11:50
  • according to screenshot it is correct to return 1,2 to you !!! the title of your nodes are 1 and 2 !!! .
    – Yuseferi
    Feb 19 '14 at 12:26
  • Yes, but how do I print as title, not integers? Please read the main thread. Later I have to send the value details on email. Integer's wont do the job Feb 19 '14 at 12:38

In case of multiple values, the return value is an array of all selected values. Therefore, $selected_index is now an array and not a string.

  • Well, then how do I display the values, because I want to send them with drupal_mail later? Feb 19 '14 at 9:28

When you use multiple the value of field is array instead of only one value you can get them :

  $selected_index = $form_state['values']['select_list']['und']; //the field is select_list
  foreach(  $selected_index as $sindex){
  • Zhilevan, thank you for your answer. The dpm($sindex) is showing the values like (0,1,2..) I want to show them as titles. Feb 19 '14 at 9:36
  • Show us the code for the select field if you please.
    – Wtower
    Feb 19 '14 at 9:41
  • Something like $selected = array_intersect_key(array_filter($form_state['values']['select_list']), $form['select_list']['#options']); will get you a filtered array of selected values, from there you can display it however you need
    – Clive
    Feb 19 '14 at 9:44
  • Wtower, I've edited my thread. Feb 19 '14 at 9:48
  • @user2519032 I update my answer
    – Yuseferi
    Feb 19 '14 at 10:27

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.