2

I have a form within a Colorbox Modal window which successfully submits using the _submit handler. I then successfully redirect the form to another page (which pre-loads a few fields on another form) using drupal_goto(). The problem I am having is that redirect stays in the Colorbox Modal window. I need to somehow close the modal window and transfer the url of the new form to the parent window. I can get this to work with a jquery function of

$(#my_form_id).submit(function (){
   parent.window.location="/user/register";
});

The problem with this issue is that the function calls when my form is submitted while disregarding anything that happens in the submission handler. So what ends up happening is I get the new page/form without the pre-loaded values until I refresh the page.

Do you have any thought or idea?

1

I would suggest using #ajax to submit the form and then allow the response to close the modal and go somewhere else. For example, here's a submit button that runs a function on click instead of submitting the form and reloading the page:

$form['submit'] = array(
  '#prefix' => '<div id="super-cool-wrapper"></div>',
  '#type' => 'button',
  '#value' => 'Submit',
  '#ajax' => array(
    'callback' => 'my_super_cool_ajax_callback',
    'wrapper' => 'super-cool-wrapper',
    'method' => 'replace',
  ),
);

And then in your callback function:

function my_super_cool_ajax_callback($form, $form_state) {
  // Do what you normally would with the form submission.
  return '<script type="text/javascript">
    parent.window.location="some/where";
  </script>';
}

Now you can process the form as needed and return a response that simply redirects the user.

  • mm thanks I will have to try this out and see if that gets the desired result. I was thinking of doing something similar to this – user975044 Apr 28 '12 at 0:10
0

Ctools have nice tools for such thinks, when you read the help file (after installing Advanced help, you can acheive this (and more :) out of the box.

very simple example code for the menu callback that prints the modal :

function mymodule_showmodal($js) {
ctools_include('modal');
ctools_include('ajax');

$form_state = array(
    'ajax' => true,
  );
$commands = ctools_modal_form_wrapper('modform',$form_state);
if ($form_state['executed']) {
  $commands = array(ctools_modal_command_dismiss());
}
print ajax_render($commands);
exit();
}

dont forget to put ['rebuild'] = true somewhere ...

0

Just had the same problem, here's how I got around it.

First I add a trigger in the $GLOBAL array like this:

function my_module_node_presave($node){
  if ($node->type == 'product'){
    $GLOBALS['redirect_parent'] = 'product_list';
  }
}

Then I add this nice event which gets triggered after the node has been saved

function my_module_drupal_goto_alter(){
  if (@$GLOBALS['redirect_parent']!=''){
    die('<script type="text/javascript">parent.window.location="/'.$GLOBALS['redirect_parent'].'";</script>');
    $GLOBALS['redirect_parent']='';
  }
}

It's not the most elegant solution, but it works...

0

I have also similar problem with drual modal form. After user registration and forgot password I have to send it on thanks page. Create tpl file and make registration link using below code.

<?php
echo l('Sign up here', 'user/register', array('query'=>array('destination'=>'thanks')));
?>

Create tpl file and make forgot password link using below code.

<?php
print l('Forgot Your Password?', 'user/password', array('query'=>array('destination'=>'thanks')));
?>
-1

for redirecting you can pass inside

function() {
  window.location.replace ="url";
}

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.