H
H
Henry Chinasky2019-02-18 15:21:37
Drupal
Henry Chinasky, 2019-02-18 15:21:37

Drupal 7. How to change select subgroup using ajax?

Good afternoon.
Question to respected Drupal 7 experts. I create a form through the Form API, I attach the code:

function sendMailChiefs_form($form, &$form_state) {
  $AllGroups = array(
    '4601' => 'TopCheif',
  );
  $keyAssocArr = key($AllGroups);
  
  $query = db_select('drupal.users', 'u');
  $query->fields('u', array('Nceh', 'FIO', 'mail'));
  $query->condition('u.Nceh', $keyAssocArr, '=');
  $result = $query->execute();

  $topChiefFIOFIO = [];
  $topChiefMail = [];
  $topChiefAll = [];
  $topChiefNumb = [];

  foreach ($result as $row) {  
    $topChiefFIO[] = $row->FIO;
    $topChiefMail[] = $row->mail;
    $topChiefNumb[] = $row->Nceh;
    $topChiefAll = array_combine($topChiefMail, $topChiefFIO);
  }

  // print_r($topChiefAll);
  $selected_top_chief = $form_state['values']['select_group'] == '4601' ? $topChiefAll : $form_state['values']['select_group'];

  $form = array();
  $form['select_group'] = array(
    '#type' => 'select',
    '#title' => 'Выберете групу пользователей:',
    '#options' => $AllGroups,
    '#description' => 'Выберите групу',
    '#empty_option' => '--Выберите групу--',
    '#required' => true,
    '#ajax' => array(
      'wrapper' => 'main-chief',
      'callback' => 'sendMailChiefs_callback',
     ),
  );
  print_r($selected_top_chief);
  $form['select_top_chief'] = array(
    '#type' => 'select',
    '#title' => 'Вопрос к членам правления',
    '#options' => $selected_top_chief,
    '#description' => 'Выберите одного из членов правления',
    '#required' => true,
    '#ajax' => array(
      'wrapper' => 'landing-cities',
      'callback' => 'sendMailChiefs_callback',
     ),
  );

  $form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => 'Отправить',
  );
  print_r($form_state['values']);
  return $form;
}

function sendMailChiefs_callback($form, $form_state) {
  return $form['select_group'];
}

How to change the select subgroup using ajax ($form['select_top_chief']).
More details. I have a group, for example, Ukraine, Russia, Belarus. When you select one of the groups, you need to pop up their areas, regions in the next select. Now it works, but you need to press submit, and I want it to be without additional. Pressing changed the second select. I hope I explained it intelligibly, if you have any questions, ask :)
PS: Thanks for the answers :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
andead, 2019-02-18
@yarik_gui

https://cgit.drupalcode.org/examples/tree/ajax_exa...

H
Henry Chinasky, 2019-02-18
@yarik_gui

Thanks, it turned out that print_r had a bad effect on code behavior :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question