I'm developing own module.

Have a form - but after submit I want to open popup and show result of submiting form in this window no at the same page with form.

How to do this?

Thank you.

Comments

RoloDMonkey’s picture

You will probably want to look at CTools. It has ways for creating AJAX forms, including modals. Pop-ups are the most common type of modals.

--

Read more at iRolo.net

Jaypan’s picture

You can do this by setting your form to submit through AJAX, then calling the popup in your AJAX response. Let's imagine the module is called form_popup

1) Create your form definition:

function form_popup_form($form, &$form_state)
{
  $form['name'] = array
  (
    '#title' => t('Enter your name'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['submit'] = array
  (
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  // We need to attach the JS file that will execute our popup
  $form['#attached']['js'][] = array
  (
    'type' => 'file',
    'data' => drupal_get_path('module', 'form_popup') . '/form_popup.js',
  );
  return $form;
}

Next, you need to ajaxify the form so it submits through AJAX. I do this through hook_form_alter, so we can set a unique wrapper on the form using the #build_id, which is not available in the original form definition. This allows your form to be used multiple times on the same page if you ever need to.

function form_popup_form_alter(&$form, &$form_state, $form_id)
{
  if($form_id == 'form_popup_form')
  {
    $wrapper_id = 'form-wrapper-' . $form['#build_id'];
    $form['#prefix'] => '<div id="' . $wrapper_id . '">';
    $form['#suffix'] => '</div>';

    $form['submit']['#ajax'] = array
    (
      'wrapper' => $wrapper_id,
      'callback' => 'form_popup_form_ajax_callback',
    );
  }
}

In the above block of code, we set the ajax callback to form_popup_form_ajax_callback(). The next thing to do is to register a command that will execute your popup in this ajax callback.

function form_popup_form_ajax_callback($form, &$form_state)
{
  $commands = array();
  $commands[] = array
  (
    // We register a custom command
    'command' => 'formPopupTriggerPopup',
    // We pass the submitted value to our command
    'name' => $form_state['values']['name'],
  );

  // We replace our entire form with a new version of the form
  // Since we've ajaxified the whole form
  $commands[] = ajax_command_replace(NULL, render($form));

  // Now we return our commands to be executed
  return array('#type' => 'ajax', '#commands' => $commands);
}

Finally, we will look at form_popup.js, which we added to the form in the first step. This is where we will add our command that will execute the popup. We need to create the callback for formPopupTriggerPopup that we set as a command in our ajax callback.

form_popup.js

(function($, Drupal)
{
  function showPopup(name)
  {
    alert(Drupal.t("Hello @name", {"@name":name}));
  }

  // Create the callback for the command we used in our ajax response
  Drupal.ajax.prototype.commands.formPopupTriggerPopup = function(ajax, response)
  {
    // response.name contains the value the user submitted in the form.
    // We will pass this to our function showPopup, so it can be shown in the popup.
    showPopup(response.name);
  };
}(jQuery, Drupal));

And there you go. Now the entire form is ajaxified, and when it is submitted, the submitted value is passed to your javscript, allowing you to create a popup that contains the submitted value.

For more information on calling JS after an AJAX submit see this tutorial I wrote: http://www.jaypan.com/tutorial/calling-function-after-ajax-event-drupal-7

Kolos_K’s picture

It works for me too by forms, but I would love to achieve it by a webform. Do somebody know the workaround?

My code looks like this now:

$node = node_load(2210);
$form = drupal_get_form('webform_client_form_2210', $node);
//I added your bit of code here:
$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Submit'),
);
$form['#attached']['js'][] = array(
  'type' => 'file',
  'data' => drupal_get_path('module', 'barion') . '/myjs2.js',
);
$myHTML =  render($form);

return $myHTML;

If instead my webform I render here your form, it's works, if the '$form['submit'] = array...' part is by the defining of the form (as in your original code).

saranya mohan’s picture

Hai

This code is working popup will come, but does not bother about validation case. Actually popup will come after the validation only, but here popup come first then only goesto the validation case

Jaypan’s picture

You can solve that by doing this:

function form_popup_form_ajax_callback($form, &$form_state)
{
  $commands = array();
  $error_messages = drupal_get_messages('error', FALSE);
  if(!$error_messages)
  {
    $commands[] = array
    (
      // We register a custom command
      'command' => 'formPopupTriggerPopup',
      // We pass the submitted value to our command
      'name' => $form_state['values']['name'],
    );
  }

  // We replace our entire form with a new version of the form
  // Since we've ajaxified the whole form
  $commands[] = ajax_command_replace(NULL, render($form));

  // Now we return our commands to be executed
  return array('#type' => 'ajax', '#commands' => $commands);
}
saranya mohan’s picture

Hai Jaypan,

Thank you for your reply .This code be solved my pblm .Now checking the validation case.

Guybrush Threepwood’s picture

This is an impressive answer. It was very helpful to me.