Ajaxified form in modal window does not dimiss I have form in modal window with two selects. Second field hidden and appears with ajax after first field selected.


If i click Submit when first select is not defined and second is
hidden - modal window dismisses; 
If i click Submit when first select
    defined and second is visible (i.e. form was refreshed with ajax),
    instead of modal dismiss i see raw output in browser:



  [{"command":"settings","settings":{"basePath":"/","pathPrefix":"","ajaxPageState":{"theme":"opie","theme_token":"5OSo2MbF_rbvJauMUEumH0Mm-gFubWX6Ai-FqvwO9lo"},"fancybox":{"options":{"padding":0,"scrolling":"visible","nextClick":1},"helpers":{"overlay":{"fixed":0}},"selectors":["a[rel=\u0022fancybox\u0022]","a.fancybox","img[rel=\u0022fancybox\u0022]",".gallery-frame
  a"]}},"merge":true},{"command":"modal_dismiss"}]


Can you help me to understand why is that?

Main code:

function modal_callback($mode = NULL) {
    if ($mode == 'ajax') {
        ctools_include('ajax'); ctools_include('modal');
        $form_state = array('ajax' =&gt; TRUE, 'title' =&gt; 'Title');

        $output = ctools_modal_form_wrapper('stage_form', $form_state);
        if (!empty($form_state['ajax_commands'])) $output = $form_state['ajax_commands'];

        print ajax_render($output); drupal_exit();
    }
    else return drupal_get_form('stage_form');
}

function stage_form($form, $form_state) {
    $form['#prefix'] = '&lt;div id="form-wrapper"&gt;';
    $form['#suffix'] = '&lt;/div&gt;';   
    $form['start'] = array(
        '#type' =&gt; 'select',
        '#options' =&gt; array('1'=&gt;'1', '2'=&gt;'2'),
        '#ajax' =&gt; array(
            'callback' =&gt; 'form_refresh',
            'wrapper' =&gt; 'form-wrapper',
        ),
    );
    if (!empty($form_state['values']['start'])) {
        $form['stop'] = array(
            '#type' =&gt; 'select',
            '#options' =&gt; array('3'=&gt;'3', '4'=&gt;'4'),
        );
    }
    $form['submit'] = array(
        '#type' =&gt; 'submit',
        '#value' =&gt; 'Submit',
    );
}

function form_refresh($form, $form_state) {
    return $form;
}

function stage_form_submit($form, &amp;$form_state) {
    $form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
I'm not familiar with that ctools command, however try:

$commands = [];

$commands[] = ajax_command_name();

return ['#type' => 'ajax', '#commands' => $commands];

What you ultimately want is for Drupal to receive and render the commands array which should trigger the dismiss and not print the commands to the screen.

(On phone, sorry for formatting)