0

I'm adding a form to the footer using hook_page_build because a want to show it using a popup, so when user launches the popup the form appears inmediatly.

The problem is that the form is not AJAX processed: the submit button hasn't the ajax-processed class so AJAX is not working. But if I render the form using hook_menu and drupal_get_form the form has the ajax-processed class and AJAX is working.

Form code fragment:

  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Your Personal Message'),
    '#default_value' => '',
    '#cols' => 50,
    '#rows' => 5,
    '#description' => '',
    '#required' => TRUE,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send Message'),
    '#ajax' => array(
      'wrapper' => 'myform-wrp',
      'callback' => 'myform_ajax_callback',
    ),
  );

Implementation of hook page build:

function mymodule_page_build(&$page) {

  // Get the current $node
  [...]

  $form = drupal_get_form('mymodule_form', $node);

  $page['footer']['my_form'] = $form;

}

Hook menu that the retrieves a working AJAX form:

function mymodule_menu() {

  $items = array();

  $items['node/%node/mymodule-form'] = array(
    'title' => 'My form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_form', 1),
    'access callback' => TRUE,
  );

  return $items;
}

I've tested it with hook_page_alter too.

What can be needed to ajaxify this form? I suppose is something related to adding the form too late...

0

The problem is the element's name: submit. In the rendered page there's another form that have the same button name (submit), so the AJAX layer just ofund one of the buttons but not the two.

To fix this just rename the element's name:

 $form['myform-submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send Message'),
    '#ajax' => array(
      'wrapper' => 'myform-wrp',
      'callback' => 'myform_ajax_callback',
    ),
  );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.