0

I have forms where I use a form alter to set various form fields on the fly. One of which is the Thank You page field that a user should go to after submitting. Example $form['submitted']['thankyou_page']['#value'] = 'my_page'; I use the same form for multiple reasons so setting the thank you page to suit.

After setting these (which work fine) I then enter a function to do some processing of the form fields to a CRM.

The final section of this function is like this

try {
    $result = $client->CreateContact($data);
} catch (Exception $e) {
    $result = $e;       
}
  my_submit_handler2($values['thankyou_page']); // redirect
}
}

The processing works fine to CRM but my problem is that the form submission (result) doesn't save in Drupal and the auto-emails after submit fail to send too. The redirect is done like this

function my_submit_handler2($f) {
  drupal_goto($f);
}

So I guess the submission is being interrupted?

How can I do these redirects without affecting the flow of the form submission?

1 Answer 1

2

A form submission handler, as well a form validation handler or a hook to alter a form, should never call drupal_goto() since it outputs a HTTP header and interrupts what Drupal is doing.

The correct way to redirect a form from a form submission handler is setting $form_state['redirect'] in Drupal 7, or calling $form_state->setRedirect() in Drupal 8. The alternative for Drupal 8 is calling $form_state->setRedirectUrl().

For Drupal 7, the following code would redirect the users to node/123?foo=bar#baz.

$form_state['redirect'] = array(
  'node/123',
  array(
    'query' => array(
      'foo' => 'bar',
    ),
    'fragment' => 'baz',
  ),
);

Your Answer

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

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