0

I want to retain dropdown list value, so that when ever someone select the option from the dropdown list should retain that value after performing the action.

/*
 *implements hook_menu().
 */

function dropdown_page_menu(){
  $items = array();
  $items['page/layout/%'] = array(
    'title' => 'Select Layout',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('dropdown_page_select_form'),
    'access arguments' => array('access content'),
    );

  return $items;
}

/**
 * Implements hook_block_info().
 */
function dropdown_page_block_info() {
    $blocks = array();

    $blocks['dropdown_page_select_form'] = array(
        'info' => t('Select Layout'),
    );

    return $blocks;
}

/**
 * Implements hook_block_view().
 */
function dropdown_page_block_view($delta = '') {
    $block = array();

    switch ($delta) {
        case 'dropdown_page_select_form':
            $block['subject'] = t('Select Layout');
            $block['content'] = drupal_get_form('dropdown_page_select_form');
            break;
    }

    return $block;
}


function dropdown_page_select_form($form, &$form_state){
  $options = array(
    'select' => t('Select'),
    'layout_1' => t('Layout 1'),
    'layout_2' => t('Layout 2'),
    'layout_3' => t('Layout 3'),
    );

  $form['choice'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#title' => t('Select your layout'),
    '#default_value' => $form_state['choice']['type']
    );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value'=> t('Go'),
    );
  return $form;
}

function dropdown_page_select_form_submit($form, &$form_state){


switch ($form_state['values']['choice']){
  case layout_1 : {

    //echo 'hi';die;
    drupal_goto("layout_1".$options);
    break;


  }
  case layout_2 : {  
    //echo 'no';die;
    drupal_goto("layout_2".$options);
    break;
  }
  case layout_3 : {  
    drupal_goto("layout_3".$options);
    break;
  }
  default: {
    print 'Select it!';
  }
}



  drupal_set_message(t('Your layout %input', array('%input' => $form_state['value']['choice'])));
}

2 Answers 2

0

Try following code. I think you need this value to be retained through all pages while navigating.

function dropdown_page_select_form($form, &$form_state) {
    $options = array(
        'select' => t('Select'),
        'layout_1' => t('Layout 1'),
        'layout_2' => t('Layout 2'),
        'layout_3' => t('Layout 3'),
    );

    $form['choice'] = array(
        '#type' => 'select',
        '#options' => $options,
        '#title' => t('Select your layout'),
        '#default_value' => isset($_SESSION['selected_layout']) ? $_SESSION['selected_layout'] : 'select'
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Go'),
    );
    return $form;
}

function dropdown_page_select_form_submit($form, &$form_state) {
    $_SESSION['selected_layout'] = $form_state['values']['choice'];
    drupal_set_message(t('Your layout %input', array('%input' => $form_state['value']['choice'])));
    switch ($form_state['values']['choice']) {
        case 'layout_1' : {

                //echo 'hi';die;
                drupal_goto("layout_1" . $options);
                break;
            }
        case 'layout_2' : {
                //echo 'no';die;
                drupal_goto("layout_2" . $options);
                break;
            }
        case 'layout_3' : {
                drupal_goto("layout_3" . $options);
                break;
            }
        default: {
                print 'Select it!';
            }
    }
}
0
0

You can set the default values as follows,

$form['choice'] = array( '#type' => 'select', '#options' => $options, '#title' => t('Select your layout'), '#default_value' => variable_get('choice', $form_state['choice']['type'] ) );

provided you get the submitted values in the variable $form_state['choice']['type'] post submit action. And the variable name should match the form field name "choice"

Hope this works for you!

1
  • it's not working Commented Feb 26, 2015 at 13:44

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.