0

I want Dropdown list A's value to filter out the options of Dropdown list B and display the number of select list as number of options of B list with same list of options.

I have written following, but using AJAX i couldn't manage to get selected value. do i missing something?

$table_list = fetch_database_tables_and_columns();
  $options = array();
  foreach ($table_list['tables'] as $option){
    $options[''] = '--Select Table--';
    $options[$option->Tables_in_test_db] = $option->Tables_in_test_db;
  }

  $selected = isset($form_state['values']['dbtables']) ? $form_state['values']['dbtables'] : key($options);
  $form['dbtables'] = array(
    '#title' => t('Table Name'),
    '#type' => 'select', 
    '#options' => $options,
    '#default_value' => $selected,
    '#prefix' => '<div class="row"><div class="col-md-6 col-sm-6 col-xs-12">',
    '#suffix' => '</div></div><div id="column-list">',
    '#ajax' => array(
      'callback' => 'ajax_view_display_dropdown_callback',
      'wrapper' => 'column-list',
    ),
  );
  //dsm($selected);
  if(!empty($selected) && $selected != 'test'){
    $column_list = fetch_database_tables_and_columns($selected);
    $col_options = array();
    foreach ($column_list['columns'] as $option){
      $col_options[''] = '--Select Column--';
      $col_options[$option->Field] = $option->Field;
    }
    foreach ($column_list['columns'] as $option){
      $form['dbcolumn'][$option->Field] = array(
        '#title' => t('Column Name'),
        '#type' => 'select', 
        '#options' => $col_options,
//        '#prefix' => '<div class="row"><div class="col-md-6 col-sm-6 col-xs-12">',
//        '#suffix' => '</div></div>',
      );
    }
  }

````
````
function ajax_view_display_dropdown_callback($form, $form_state){
  return $form['dbcolumn'];
}
````

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.