1

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?

EDIT:

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)
   dpm($sindex);
}
14
  • please dpm($form_state['values']['select_list']) in submit and get snap shot of nested row. tnx
    – Yuseferi
    Commented Feb 19, 2014 at 11:42
  • Here's a screenshot imageshack.com/a/img827/3339/4vea.jpg Commented Feb 19, 2014 at 11:48
  • This is when a select the second and third options in the list. Commented Feb 19, 2014 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
    Commented Feb 19, 2014 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 Commented Feb 19, 2014 at 12:38

2 Answers 2

2

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.

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

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
  $values=array();
  foreach(  $selected_index as $sindex){
   $values=$sindex['value'];
   //dsm($sindex['value']);
}
9
  • Zhilevan, thank you for your answer. The dpm($sindex) is showing the values like (0,1,2..) I want to show them as titles. Commented Feb 19, 2014 at 9:36
  • Show us the code for the select field if you please.
    – Wtower
    Commented Feb 19, 2014 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
    Commented Feb 19, 2014 at 9:44
  • Wtower, I've edited my thread. Commented Feb 19, 2014 at 9:48
  • @user2519032 I update my answer
    – Yuseferi
    Commented Feb 19, 2014 at 10:27

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.