0

I have a form for which I want to theme with a template file, and I also need to use ajax in it. But I get this error messages in the log and ajax doesn't work :

  • message 1

    Invalid form POST data.

  • message 2

    Notice: Undefined index: form_build_id in ajax_get_form() (line 320 of /Applications/MAMP/htdocs/ta/includes/ajax.inc).

ajax_test.module :

function ajax_test_menu() {
    return array(
        'ajax_test' => array(
            'title' => t('Ajax test'),
            'page callback' => 'drupal_get_form',
            'page arguments' => array('_ajax_test_form'),
            'access arguments' => array('access content'),
        )
    );
}

function ajax_test_theme() {
    return array(
        '_ajax_test_form' => array(
            'template' => 'ajax_test-form',
            'render element' => 'form',
        ),
    );

}

function _ajax_test_form() {
    return array(
        'name' => array(
            '#type' => 'select',
            '#id' => "select-id",
            '#options' => range(1, 40),
            '#ajax' => array(
                'callback' => '_ajax_test_callback',
                'wrapper' => "more-options",
            ),
        ),
    );
}

function _ajax_test_callback($form, $form_state) {
    return '<h1>Hello world!</h1>';
}

ajax_test-form.tpl.php :

<div>
    <? echo drupal_render($form['name']); ?>
</div>
<div style="height: 20px; border: 2px dashed blue" id="more-options"></div>
3
  • 2
    Is that you're entire template file there? If so, you're missing a print drupal_render_children($form);
    – Clive
    Commented Jul 17, 2013 at 14:03
  • I didn't knew about that function. But what I want is to render each field separately Commented Jul 17, 2013 at 15:12
  • That's fine, just make sure you use that line at the bottom of the template file. It'll only render elements that haven't already been renedered. Otherwise you'll have no CSRF tokens/etc in the form, and it won't work
    – Clive
    Commented Jul 17, 2013 at 15:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.