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