Hi all,

I'm currently tring to load multiple form elements with a ajax callback, this is working partly.

First there is a select box with the 'type' choose, after a user has chosen the type another select box will appear and the user can choose a more specific option related to the first option. After this last option, multiple form elements need to be loaded depending on the user choice. At this moment the first AJAX call works perfect. Only the second call doesn't work correctly, I'm getting some form elements returned but when I change a option the stay on the screen (elements for a option don't disapear when choosing another option).

<?php
//form
function add_assignment_event($form, &$form_state, $verion_id){
   
//this AJAX fucntion works perfect
   
$form['choose_type'] = array(
       
'#type' => 'select',
       
'#title' => 'Type',
       
'#options' => drupal_map_assoc(array(t('Choose type'),
                                            
t('Assignment'),
                                            
t('Event'))),
       
'#ajax' => array(
           
'callback' => 'type_input_ajax_callback',
           
'wrapper' => 'input_type_wrapper',
        ),
    );
   
   
$form['type'] = array(
       
'#type' => 'markup',
       
'#title' => '',
       
'#prefix' => '<div id="input_type_wrapper">',
       
'#suffix' => '</div>',
    );
   
   
$form['open_question_question'] = array(
       
'#type' => 'markup',
       
'#prefix' => '<div id="open_question_guestion_wrapper">',
       
'#suffix' => '</div>',
    );
   
   
$form['open_question_check_answer'] = array(
       
'#type' => 'markup',
       
'#prefix' => '<div id="open_question_check_wrapper">',
       
'#suffix' => '</div>',
    );
   
   
$form['goto_message'] = array(
       
'#type' => 'markup',
       
'#prefix' => '<div id="goto_message">',
       
'#suffix' => '</div>',
    );
   
    if(!empty(
$form_state['values']) && strcmp($form_state['values']['choose_type'], t('Choose type'))) {
        if(!empty(
$form_state['values']) && $form_state['values']['choose_type'] == 'Assignment') {   
           
$form['type']['#type'] = 'select';
           
$form['type']['#title'] = t('Assignment type');
           
$form['type']['#options'] = drupal_map_assoc(array(t('Choose assignment'),
                                                       
t('Open question'),
                                                       
t('GoTo'),
                                                       ));
           
$form['type']['#ajax'] = array(
               
'callback' => 'assignment_ajax_callback',
            );
            if(!empty(
$form_state['values']) && strcmp($form_state['values']['type'] t('Open question'))){//1
               
$form['open_question_question']['#type'] = 'textfield';
               
$form['open_question_question']['#title'] = t('Question');
               
$form['open_question_check_answer']['#type'] = 'checkbox';
               
$form['open_question_check_answer']['#title'] = t('Check answer');
            }
            if(!empty(
$form_state['values']) && strcmp($form_state['values']['type'], t('GoTo'))){//1
               
$form['goto_message']['#type'] = 'textarea';
               
$form['goto_message']['#title'] = t('Message');
            }



        }
        if(!empty(
$form_state['values']) && $form_state['values']['choose_type'] == 'Event') {
           
$form['type']['#type'] = 'select';
           
$form['type']['#title'] = t('Event type');
           
$form['type']['#options'] = drupal_map_assoc(array(t('Choose event'),
                                                              
t('Counter'),
                                                              
t('if/ else')));
           
$form['type']['#ajax'] = array(
               
'callback' => '',
               
'wrapper' => '',
            );
        }
    }
}
?>

I've tried multiple things in the callback function as you can see below..

<?php
//Callback
function assignment_ajax_callback(&$form, $form_state){
    if(!empty(
$form_state['values']) && strcmp($form_state['values']['type'], t('Open question'))){
        return array(
           
'#type' => 'ajax',
           
'#commands' => array(
                 
ajax_command_replace("#open_question_guestion_wrapper", render($form['open_question_question'])), //*
                 
ajax_command_replace("#open_question_check_wrapper", render($form['open_question_check_answer'])),
            )
          );
    }
    elseif(!empty(
$form_state['values']) && strcmp($form_state['values']['type'], t('GoTo'))){
        return array(
           
'#type' => 'ajax',
           
'#commands' => array(
               
ajax_command_replace("#goto_message", render($form['goto_message'] )),
                )
          );
    }
}
?>

*Strict warning: Only variables should be passed by reference in assignment_ajax_callback()

Can someone help me?

Thanks,
Rik

Comments

RikD’s picture

I've changed some code. I've added the if's at *1. And removed some lines in the callback function so the error is gone there. But now I'm getting
Notice: Undefined index: type in.. at *1

Also the old form elements don't go away when selecting a different option.

Edit:
Also when selecting the first item, the second item shows up. And the other way around.

RikD’s picture

Ok, it's getting there, but stil a few issues

<?php
//form (other code see #1 post)
if(!empty($form_state['values']) && strcmp($form_state['values']['choose_type'], t('Choose type'))) {
    if(!empty(
$form_state['values']) && $form_state['values']['choose_type'] == 'Assignment') {   
       
$form['type']['#type'] = 'select';
       
$form['type']['#title'] = t('Assignment type');
       
$form['type']['#options'] = drupal_map_assoc(array(t('Choose assignment'),
                                                          
t('Open question'),
                                                          
t('GPS GoTo'),
                                                          ));
       
$form['type']['#ajax'] = array(
           
'callback' => 'assignment_ajax_callback',
        );
           
        if(!empty(
$form_state['values']) && strcmp($form_state['values']['type'], t('Open question'))){
           
$form['goto_message']['#type'] = 'markup';
           
$form['open_question_question']['#type'] = 'textfield';
           
$form['open_question_question']['#title'] = t('Question');
           
$form['open_question_check_answer']['#type'] = 'checkbox';
           
$form['open_question_check_answer']['#title'] = t('Check answer');
        }
        if(!empty(
$form_state['values']) && strcmp($form_state['values']['type'], t('GPS GoTo'))){
           
$form['open_question_question']['#type'] = 'markup';
           
$form['open_question_check_answer']['#type'] = 'markup';
           
$form['goto_message']['#type'] = 'textarea';
           
$form['goto_message']['#title'] = t('Message');
        }
   
    }
    if(!empty(
$form_state['values']) && $form_state['values']['choose_type'] == 'Event') {
       
$form['type']['#type'] = 'select';
       
$form['type']['#title'] = t('Event type');
       
$form['type']['#options'] = drupal_map_assoc(array(t('Choose event'),
                                                     
t('Counter'),
                                                   
t('if/ else')));
       
$form['type']['#ajax'] = array(
           
'callback' => '',
           
'wrapper' => '',
        );
    }
}
?>
<?php
//callback
function assignment_ajax_callback(&$form, $form_state){
    return array(
       
'#type' => 'ajax',
       
'#commands' => array(
           
ajax_command_replace("#goto_message", render($form['goto_message'])),
             
ajax_command_replace("#open_question_guestion_wrapper", render($form['open_question_question'])),
             
ajax_command_replace("#open_question_check_wrapper", render($form['open_question_check_answer'])),
        )
    );
}
?>

Not sure if this is the way to do it, but most of it is working now. Only still getting the error of displaying the wrong elements. When chosing the fist option, the elements for the second option is shown and the other way around...