Need help on adding options to $form via select field I altered the username field in login block into a select field and now I'm trying to filter the usernames by their user roles via another Select Field.

function design_form_alter(&amp;$form, $form_state, $form_id){

    $users = entity_load('user');
    $user_names = array();
    $acc = array("PH","SL");
    $rolePH = array();
    $roleSL = array();
    foreach ($users as $user_id =&gt; $user) {
    $user_names[] = $user-&gt;name;
      if (in_array('PH', array_values($user-&gt;roles))) {
        $rolePH[] = $user-&gt;name;
      }
      elseif (in_array('SL', array_values($user-&gt;roles))) {
        $roleSL = $user-&gt;name;
      }
    }
    $user_arr = array_combine($user_names, $user_names);
    if($form_id=="user_login"){
    $form['roles']['#type'] = "select";    
    $form['roles']['#weight'] = -0;
    $form['roles']['#size'] = 'auto';
    $form['roles']['#options'] = $acc;
    $form['roles']['#title'] = t('Role');
    $form['name']['#type'] = "select";
    $form['name']['#options'] = $user_arr;
    $form['name']['#required'] = true;
    $form['name']['#size']= 'auto';
    $form['name']['#title']= t('Organization');

    }
}


I'm a bit stuck, as you can see, I already separated users depending on their roles. My problem is how do I set the $form['name'] options when a role is selected. Right now I'm just showing all users in the DB.
Borrowed From HERE:

If you want to do this hardcoded, you might try to use the #ajax property of the Form API

You can reload your second dropdown on change of the value of the first dropdown and change the value while reloading the form.

I made a little example to give you an idea :

$form['number'] = array(
  '#type' =&gt; 'select',
  '#title' =&gt; t('Select Number'),
  '#options' =&gt; array(
      0 =&gt; 1,
      1 =&gt; 2,
      2 =&gt; 3,
  ),
  '#default_value' =&gt; 0,
  '#ajax' =&gt; array(
    'callback' =&gt; 'ajax_replace_dropdown_default_value_callback',
    'wrapper' =&gt; 'wrapper-dropdown',
    'method' =&gt; 'replace',
    'effect' =&gt; 'fade',
  ),
);

$default_value = !empty($form_state['values']['number']) ? $form_state['values']['number'] : 0;

$form['open_closed'] = array(
  '#type' =&gt; 'select',
  '#title' =&gt; t('Open / Closed ?'),
  '#options' =&gt; array(
      0 =&gt; t('Open'),
      1 =&gt; t('Closed'),
      2 =&gt; t('Progress'),
  ),
  '#value' =&gt; $default_value,
  '#prefix' =&gt; '&lt;div id="wrapper-dropdown"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;',
);

function ajax_replace_dropdown_default_value_callback($form, $form_state) {
  return $form['open_closed'];
}

In your case you will have to do it in a hook_form_alter and add the #ajax property to your first dropdown and add prefix and suffix to give the second dropdown the proper value.

You can also check this link about AJAX with the FORM API :