I have a form in which user needs to input his height. I have two radio buttons to select the format in which the user wishes to input his height. one is "feet-inch" and another is "centimeters". I want to show a dropdown if "feet-inch" is selected, or else a textfield if "centimeters"is selected using ajax.
Am being able to show the textfield and a single dropdown successfully with ajax call triggered from the selection of the radio buttons, But I need to have two dropdowns, one to select "feet" and another to select "inches" right..?
So my problem is that am not being able to show two dropdowns with one single ajax call. How can I achieve it...?? Or in other words, how can I replace two separate divs (both containing a dropdown select) with one single ajax call..?
Alseo do let me know, if there are any other better ways to achieve this kind of height selection form..?
Please provide, codes for both form elements and the ajax call if possible.
Thanks in advance.
//// EDIT /////
I put both select fields inside a fieldset and wrapped that fieldset with the wrapper to be replaced. and put it inside the something_form() function with an if statement. I dont know where am going wrong... Below is my code, please show me wats wrong....
<?php
function my_form($form,&form_state)
{
$form['health_set']['height_set'] = array(
'#title'=>t('Input your height'),
'#type'=>'fieldset',
'#tree'=>TRUE,
);
$form['health_set']['height_set']['height_radio'] = array(
'#title' => t("Height input format"),
'#type' => 'radios',
'#options' => array(
1 => t('feet'),
2 => t('centimeters'),
),
'#required'=>TRUE,
'#ajax' => array(
'callback' => 'health_height_callback',
'wrapper' => 'health_height-wrapper',
'effect'=>'slide',
),
);
if($form_state['values']['health_set']['height_set']['height_radio']==1)
{
$form['health_set']['height_set']['feet-inch'] = array(
'#type'=>'fieldset',
'#tree'=>TRUE,
'#prefix'=>'<div id="health_height-wrapper">',
'#suffix'=>'</div>',
);
$options1 = array(
1=>t('1 ft'),
2=>t('2 ft'),
3=>t('3 ft'),
4=>t('4 ft'),
5=>t('5 ft'),
6=>t('6 ft'),
7=>t('7 ft'),
);
$form['health_my_bmi']['height_set']['feet-inch']['feet'] = array(
'#type'=>'select',
'#options'=>$options1,
'#required'=>TRUE,
'#description'=>t('Whats your height in feet?'),
);
$options2 = array(
1=>t('1 inch'),
2=>t('2 inch'),
3=>t('3 inch'),
4=>t('4 inch'),
5=>t('5 inch'),
6=>t('6 inch'),
7=>t('7 inch'),
8=>t('8 inch'),
9=>t('9 inch'),
10=>t('10 inch'),
11=>t('11 inch'),
);
$form['health_set']['height_set']['feet-inch']['inch'] = array(
'#type'=>'select',
'#options'=>$options2,
'#title'=>t('Inches'),
'#description'=>t('how many inches ?'),
'#required'=>TRUE,
);
}
else if($form_state['values']['health_set']['height_set']['height_radio']==2)
{
$form['health_set']['height_set']['centi'] = array(
'#type'=>'textfield',
'#title'=>'Height in centimeters',
'#prefix'=>'<div id="health_height-wrapper">',
'#suffix'=>'</div>',
'#maxlength'=>3,
'#size'=>6,
'#required'=>TRUE,
);
}
return $form;
}
?>
below is the code of the callback function,
<?php
function health_height_callback($form, $form_state)
{
if($form_state['values']['health_set']['height_set']['height_radio']==1)
return $form['health_set']['height_set']['feet-inch'];
else if($form_state['values']['health_set']['height_set']['height_radio']==2)
return $form['health_set']['height_set']['centi'];
}
?>