计算填充的文本字段以填充Drupal 7表单中的选择输入中的选项

I would like to be able to count the number of text inputs that have been filled and use the result to create a drop down with the options 0-[count returned]. I am using the ajax_command_replace function in Drupal 7 but open to suggestions of any other method.

I have a test which does count the inputs filled and displays the result but I cannot figure out how to then use this to populate a select.

Any help much appreciated.

for the form elements.

$array = array_fill(0,5,'');

$form['test'] = array(
'#type'=> 'fieldset',
'#title' => 'TEST',
);
$form['test']['value']['#tree'] = TRUE;

foreach ($array as $key => $value) {
$form['test']['value'][$key] = array(
    '#type' => 'textfield',
    '#ajax' => array(
      'callback' => 'test_callback',

    ),
);

}
$test = count(array_filter($array));
$form['test']['count'] = array(

    '#suffix' => "<div id='testcount'>filled inputs = $test</div>",
);

and the ajax callback

function test_callback($form, $form_state){
    $text = count(array_filter($form_state['input']['value']));

    $commands = array();
    $commands[] = ajax_command_replace("#testcount", "<div id='testcount'>filled inputs = $text</div>");
    return array('#type' => 'ajax', '#commands' => $commands);
}

Thanks

This seems to be working for me now, same method but using the render() function to return an updated form element from within the callback. I hope it helps someone else.

$array = array_fill(0,5,'');

                $form['test'] = array(
                    '#type'=> 'fieldset',
                    '#title' => 'TEST',
                );
                $form['test']['value']['#tree'] = TRUE;

                foreach ($array as $key => $value) {
                    $form['test']['value'][$key] = array(
                        '#type' => 'textfield',
                        '#ajax' => array(
                            'callback' => 'test_callback',                      
                        ),
                    );

                }
                $options = range(count(array_filter($array)),0,1);
                $form['test']['count'] = array(
                    '#type'=> 'select',
                    '#options' => $options,
                    '#prefix' => '<div id="testcount">',
                    '#suffix' => "</div>",

                );

Callback

function test_callback($form, $form_state){
$text = count(array_filter($form_state['input']['value']));
$default = $form_state['input']['count'];
$options = range($text,0,1);
$form['test']['count'] = array(
    '#type'=> 'select',
    '#options' => $options,
    '#prefix' => '<div id="testcount">',
    '#suffix' => "</div>",
    '#default_value' => $default,
);

   $commands = array();
   $commands[] = ajax_command_replace("#testcount", render($form['test']['count']));

   return array('#type' => 'ajax', '#commands' => $commands);
}