I have a custom form in a module, similar to this example:
function mymodule_form($form, &$form_state) { $radio_options = array(...); $form = array(); $form["wrapper"] = array("#markup" => "<div id='test-ajax'></div>"); $form['choice'] = array( '#title' => '', '#type' => 'radios', '#options' => $radio_options, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Vote'), '#ajax' => array( 'callback' => 'demo_form_ajax_submit', 'wrapper' => "test-ajax", 'method' => 'replace', 'effect' => 'fade', ), ); return $form; } function demo_form_ajax_submit($form, $form_state) { var_dump("something"); }
And I’m printing that form in template like this:
<div class='row'> <?php print render($form['choice']); ?> <?php print render($form['form_build_id']); ?> <?php print render($form['form_token']); ?> <?php print render($form['form_id']); ?> <?php print render($form['submit']) ?> </div>
The form is printed correctly and even the button shows the ajax icon when clicked, but it does not work. When I check the logs it shows 2 errors:
- Notice: Undefined index:
form_build_id in ajax_get_form()
(line 325 of… - Invalid form POST data.
It is similar to this issue Ajax not working for themed form but it does not work even adding drupal_render_children($form);
at the bottom of the template.
Notes:
- I added
drupal_add_library('system', 'drupal.ajax');
in the module. - I’m using jQuery 1.7
- I also tested
drupal_render()
instead ofrender()
.