Ajax form only works the first time I have this simple ajax form with some ajax taxonomy checkboxes and a function that renders the content when the users changes the selected checkboxes. Everything work fine, the first time. The second time the user changes a checkbox I get this error:

Undefined index: form_build_id in ajax_get_form() (line 320 of /var/www/mf/includes/ajax.inc) and Invalid form POST data.

The only strange thing I've seen is when I inspect the request, there are a settings command, and ok insert command and another empty insert:

{command:insert, method:prepend, selector:null, data:, settings:null}


And in my html there is an empty . 

My form:

function courses_lists_form($form, &amp;$form_state) {dd(date('l jS \of F Y h:i:s A'));
  $terms = taxonomy_get_tree(1); // 1 = courses taxonomies vid
  $terms_array = array();
  foreach ($terms as $term) {
    $terms_array[$term-&gt;tid] = $term-&gt;name;
  }
  $form['taxonomias'] = array(
    '#type' =&gt; 'checkboxes',
    '#options' =&gt; $terms_array,
    '#title' =&gt; t('TEMÁTICAS'),
    '#ajax' =&gt; array(
      'callback' =&gt; 'courses_lists_form_ajax',
      'wrapper' =&gt; 'courses-lists-ajax',
    ),
  );
  $form['block'] = array(
    '#prefix' =&gt; '&lt;div id="courses-lists-ajax"&gt;',
    '#markup' =&gt; mf_form_commons_courses_lists_gen(0, 0, TRUE, 'matrix', array(0 =&gt; 9), ''),
    '#suffix' =&gt; '&lt;/div&gt;',
  );
  $form_state['redirect'] = FALSE;
  return $form;
}

function courses_lists_form_ajax($form, &amp;$form_state){
  $taxs = array();
  foreach ($form_state['values']['taxonomias'] as $tid) {
    if($tid) $taxs[] = $tid;
  };
  $outp = '&lt;div id="courses-lists-ajax"&gt;';
  $outp .= mf_form_commons_courses_lists_gen(0, 0, TRUE, 'matrix', $taxs, '');
  $outp .= '&lt;/div&gt;';
  return $outp;
}
There was an error in the generated markup. A missing closing div was messing everything. Now everything work as expected. Thanks.