I have a form which send an ajax request to my server. The form is:

function dokimi_add_to_list_form($form, $form_state) {

  $form = array();



  $form['private_list'] = array(
    '#type' => 'radios',
    '#title' => 'Private lists',
    '#options' => $options,
    '#prefix'=>'<div id="list_list">',

  $form['add_new']= array(
    '#type' => 'fieldset',
    '#markup' => 'Add a list'

  $form['add_new']['new_list']= array(
    '#type' => 'textfield',
    '#title'=> 'New list',
    '#default_value' => '',

  $form['add_new']['add']= array(
    '#type' => 'button',
    '#value' => t('Add'),
              'callback' => 'add_to_list_callback',
                  // 'wrapper' is the HTML id of the page element that will be replaced.

 return $form;

And the callback function is:

function add_to_list_callback($form, $form_state){

    $form['private_list'] = array(
        '#type' => 'radios',
        '#title' => 'Private list',
        '#options' => $options,
        '#prefix'=>'<div id="list_list">',

    $commands = array();
    $commands[]=ajax_command_replace('#list_list', render($form['private_list']));
    return array(


When the 'Add' button is clicked, a new array of options should be returned, but I get an empty list instead.

The form is rendered in a ctools modal if that is of importance.

Thanks for any help with this - it's driving me barmy.

1 Answer 1


I think you need to reorganize your code. First you need two ['Private_list'] at the same function Form and at this same function form you need to declare the array variables for each private list.

$list1_options = array(......);
$list2_options = array(.......);

['Private_list1´]: these are the first choice the user need to do: here you use the ajax call back.

['Private list2']: these one have the choices the user need to pick according to the first selection on ['Private_list1´]: at this 'Private_list2' you use the wrapper prefix and suffix.

the ajax call back function should only look something like this most of the times:

function example_change_callback($form, &$form_state) {
  return $form['private_list2'];

Your basicly using the call back to identify wich form you need to update on event change.

I recommend you study this drupal dependent ajax example, dispate the example is using dropdown list, you should be able to aply it to radio buttons, just follow the same structure.

I hope i was hepfull, tell us later how it went... Have a good coding!

  • Yes thanks Hawk. I did finally work this out. I was trying to construct the form element in my callback, whereas it should be done in the main form function. the options were just to illustrate it, the main point is the callback.
    – gillarf
    Mar 15, 2016 at 9:23
  • Glad to hear that! Mar 15, 2016 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.