1

How can I display the form after ajax call and closing the modal? Right now when I use ajax to display the custom message ,but it preserves the state. When I click on modal button it display the same custom message but not the form

My code is as follows:

function demo_demo_form($form, &$form_state) {
  return array(
    'email' => array(
      '#type' => 'textfield',
      '#title' => t('Join our Newsletter'),
      '#required' => TRUE,
      '#attributes' => array(
        'placeholder' => t('mail@example.com'),
      ),
    ),
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Subscribe'),
      '#ajax' => array(
        'callback' => 'demo_form_ajax_submit',
        'wrapper' => 'demo-demo-form',
        'method' => 'replace',
        'effect' => 'fade',
      ),
    ),
  );
}

/**
 * Ajax callback function.
 */
function demo_form_ajax_submit($form, $form_state) {
// Dummy/dumb validation for demo purpose.
  if (!empty($form_state['input']['email'])) {
    $message = "subscription form submitted!";
    return $message;
  }
  else {
    return $form;
  }
}

But when I click modal button it shows the same message. I can use window.location.reload but I only want to reload the form in the block.

Javascript is as follows:

// On Load
(function($) {
  Drupal.behaviors.demo = {
    attach: function (context, settings) {
      $('#mybutton').on( "click", function() {
        $('#block-demo-file-upload').modal('hide');
      });
    }
  };
})(jQuery);

How can I display the form when I open the modal window next time without reloading the page?

  • You are not returning $form inside hook_form_submit(). $form is inside the else so when ever the email value exist it will not return $form. – inizio Mar 13 '14 at 6:55
  • yes but how to display the message and when the modal close render the form? – Aditya R Joshi Mar 13 '14 at 20:51

Your Answer

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

Browse other questions tagged or ask your own question.