Here's the issue I'm facing. I need to have a second submit button on a webform, with a different #value. I added the new submit button using hook_form_alter but now the form isn't submitted, because of that #value of the button.

Is there a way to achieve this functionality?

Comments

manuelBS’s picture

if you add a button in hook_form_alter with

$form[mysubmit] = array(
  '#type' => 'submit',
  '#value' => t('Submit otherwise'),
);

you could add a validation handler and a submission handler to your form to check for the button. Do this in hook_form_alter, too

$form['#validate'][] = '_my_validation_handler_function'

so implement the function _my_validation_handler_function and as for your button like

function _my_validation_handler_function($form, &$form_state){
  $values = $form_state['values'];
//check with key 'op' which button was pressend.
}

You can do the same using a submit handler with '#submit' in your form, see also http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....

Hope this will help!

quicksketch’s picture

Title: additional submit buttons » Additional submit buttons
Category: feature » support

The '#value' of your new needs to be the same as the existing Submit button, since webforms may have multiple buttons (Previous, Next, Save as Draft, and Submit). You can change the label of the submit button to whatever you want under the advanced settings at node/x/webform/settings, then the form will submit.

mariusilie’s picture

@manuelBS
I know know how to do this for every normal form, but this doesn't work for webforms. Thanks anyway :)

@quicksketch
I know I can change the label of the submit button in the advanced settings, but what I need is to have an additional submit button that saves the info as the regulat submit button, but also to save some other infos into another table.

mariusilie’s picture

I guess the problem is on line 1926 in webform.module.

This line:

  if (!in_array($form_state['values']['op'], array($submit_op, $draft_op))) {...}

Should be something like this:

  $permitted_op = array($submit_op, $draft_op);
  $permitted_op = array_merge($permitted_op, $additional_submit_op);
  if (!in_array($form_state['values']['op'], $permitted_op)) {...}

where $additional_submit_op would be an array of values you can set under the Advanced settings of the webform.

quicksketch’s picture

I'm pretty sure we're not going to add end-user options to the webform configuration that wouldn't have any effect. Any changes that assist the processing of forms should probably be changed as part of the hook_form_alter().

Regarding the existing code, my point was that you can have two submit buttons (one additional one added by hook_form_alter()), but currently the one you add via hook_form_alter() needs to have the same #value as the one that already exists on the page. So if you want yours to say something other than "Submit", you can change that under the advanced settings.

quicksketch’s picture

Status: Active » Closed (fixed)

Closing after lack of activity. Newer versions of Webform handle the submit button values better, allowing them to be changed through hook_form_alter() if desired.

mikeejt’s picture

I don't mean to reopen the issue, but I don't want to create a duplicate one. I'm trying to do the same as mariusilie, having 2 separate submit buttons that have the same behavior (saves webform data to the db, and url redirects).

There are two differences between the two buttons:
1) Text (therefore #value) needs to be different for the two buttons
2) URL redirect is different.

The closest I've come to somebody achieving this is found here: http://agileadam.com/webform-multiple-submit-buttons

But they're using jQuery to hack the txt of the button. And it's not doing a URL redirect. The jQuery part I'm fine w/, but I'm not sure on how to approach the URL redirect. In the end, I would prefer to do it the "correct" way in hook_form_alter() and not partly through a jQuery hack.

Can somebody make a recommendation?

mikeejt’s picture

Status: Closed (fixed) » Active

Edit - forgot to change Status to "active".

mikeejt’s picture

For reference, I saw this post: http://drupal.org/node/1332430

And tried the fix recommended quicksketch, but still didn't work.

quicksketch’s picture

Status: Active » Closed (fixed)

What you're trying to accomplish is beyond the scope of Webform's functionality.

As mentioned in the submission guidelines, I don't provide help with custom coding:

Any issues regarding "how do I code ..." or "how do I theme ..." will not be answered. Please look elsewhere for coding resources.

Please refer to other resources such as IRC or Drupal StackExchange to ask your questions.