1

I have a custom user registration form (D7). I added two custom submit buttons inside THEME_form_alter

  $form['actions']['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Sign Up')
  );
  $form['actions']['submit2'] = array(
    '#type' => 'submit', 
    '#value' => t('Sign Up & Take Survey')
  );

These render fine and both handle the registration equally well but when submit2 is clicked I want to redirect to a webform survey.

I added the following:

function tweakform_user_register_submit($form, &$form_state) {

  if($form_state['values']['op'] == $form_state['values']['submit2']) {
    echo 'Redirect to survey and populate fields with user details';
    exit;
  }  

}

I refreshed the cache and this function does not seem to be invoked.

Alex

  • Well, I see now that you need the form to submit your data regardless of the button they push, sorry for that oversight. What is the name of your function that generates the form? I don't see why your if() wouldn't be successful. – Chris Rockwell Jul 3 '13 at 19:52
3

I'd suggest using the #submit property

For example:

$form['actions']['submit'] = array(
 '#type' => 'submit', 
 '#value' => t('Sign Up')
);
$form['actions']['submit2'] = array(
  '#type' => 'submit', 
  '#value' => t('Sign Up & Take Survey')
  '#submit' => array('custom_submit_for_this_button')
);

And then:

function your_form_name_submit($form, &$form_state) {
  // your logic for first button here; this is auto-called by Drupal
}
function custom_submit_for_this_button($form, &$form_state) {
  // your logic for second button here
}
  • this is the way to go. The default form handlers *_submit and *_validate are just that defaults -- but you dont have to use them at all. When the defaults no longer work cleanly for you; you leverage other tools like #submit. – tenken Jul 3 '13 at 19:41
  • OK cool. But in the case of submission I want both buttons to carry out the default and only in the case of the second button do I want to redirect to a webform. I assume I can just call the defaults first? – Alex.Barylski Jul 3 '13 at 19:43
0

Inside your form submit for submit2 button you should use redirect as follows.

function your_form_name_submit($form, &$form_state) {
 $form_state['redirect'] = 'path_to_your_webform_survey';
}
-1
$form['actions']['submit2'] = array(
  '#type' => 'submit', 
  '#value' => t('Sign Up & Take Survey'), //the missing line in above example
  '#submit' => array('custom_submit_for_this_button')
);

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.