Ajax form works on page but not on block I've created an ajax form with drupal API. The ajax functionality works if I render the form on a page with hook_menu, but it doesn't when I render it in a block. This is how I create my block:

function hook_block_info() {
    $blocks['my_block'] = array(
      'info' =&gt; t('Title'),
      'cache' =&gt; DRUPAL_NO_CACHE,
    );

    return $blocks;
}

function hook_block_view($delta = '') {
    $block = array();

    switch ($delta) {
        case 'my_block':
            $block['subject'] = t('Title');
            $block['content'] = drupal_get_form('my_form');
            break;
    }
    return $block;
}


Any idea how I can fix this?
Did you create the form in a separate file?  If you did, then it makes a big difference where in the main .module file you put the module_load_include() function to bring in the form.  I had this problem because my form definition was in a .inc file, but when I moved module_load_include() from hook_node_view() to hook_init(), it worked fine.

Edit:  Wait, better answer:  hook_init() isn't called on cached pages, so may not be the way to go.  Instead, call module_load_include() wherever it makes sense (e.g., hook_node_view() or hook_block_view()), but use form_load_include() within your form definition, like this:

function my_ajax_test_node_form($form, &amp;$form_state) {
  form_load_include($form_state, 'inc', 'my_ajax_test', 'my_ajax_test.forms');
  $form['my_ajax_test_div'] = array(
    '#type' =&gt; 'container',
    '#tree' =&gt; TRUE,
    '#prefix' =&gt; '&lt;div id="my-ajax-test-form-div"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
  );
  return $form;
}


Once the file has been loaded with form_load_include(), it will be loaded anytime the form is called.