欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Webform上的多个提交按钮

Webform上的多个提交按钮

多个网络表单提交我花了很多时间使用hook_form_alter()函数来修改表单,但是前几天我被一个简单的问题困扰了。我需要在表单上有两个提交按钮。默认情况下,在Webform的最新分支中,您可以更改webform的默认提交按钮上的文本标签。如果您查看右侧的屏幕截图,您将看到我的演示表单的样子。“请求更多信息”按钮是默认的提交按钮。“推荐朋友”按钮是我正在展示如何通过hook_form_alter()添加的按钮

我们来谈谈第二个提交按钮的原因。表格的规格显示了两个按钮。如果用户点击“推荐朋友”,表单将提交另外一条信息,说明用户是指朋友。如果单击“请求更多信息”按钮,将提交表单并且“由朋友推荐”状态将为“否”。    

首先,我创建了一个名为“引用朋友”的隐藏字段。我为此字段设置了默认值“no”。围绕此字段没有任何依赖关系或逻辑,所以我没有打扰1或0,TRUE或FALSE。我希望确认电子邮件说“是”或“否”。    

接下来,使用hook_form_alter(),我注入了一个额外的提交按钮。这是代码:    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**                                
* Implementation of hook_form_alter().                                
*/                                
function mymodule_form_alter(&$form, &$form_state, $form_id) {                                
  switch ($form_id) {                                
  case 'webform_client_form_36':                                
    $form['refer_friend_btn'] = array(                                
      '#type' => 'submit',                                
      // Text replaced with jQuery (see script.js); couldn't figure out                                
      // how to submit normally with a different #value other that of                                
      // the default submit button                                
      '#value' => t('Request More Information'),                                
    );                                
    $form['#validate'][] = 'mymodule_get_started_form_validate';                                
    break;                                
  }                                
}                                

所以,如果你仔细阅读了一小段代码,你会看到我对提交按钮的#value属性的评论。为了使表单“正常”处理,如果单击默认提交按钮,我必须使按钮具有相同的值。我花了很多时间试图解决这个问题,对于我正在做的项目而言,根本不值得花更多的时间来调查。我很尴尬地说,但这些都是事实!    

现在,如果我在此时查看表单,我将有两个看似相同的按钮。我们将使用jQuery重新标记新的标签。整个站点需要运行jQuery,所以我不关心这个实现。我失去了开发者街头信誉吗?哈哈。    

截至目前,如果您点击“推荐朋友”按钮,则不会发生任何特殊情况。表格将照常提交。所以,让我们添加一个验证处理程序来设置隐藏的“引用朋友”元素的值。    

1
2
3
4
5
6
7
8
9
/**                                
* Additional validation handler for the Get Started form.                                
*/                                
function mymodule_get_started_form_validate($form, &$form_state) {                                
  if ($form_state['clicked_button']['#post']['op'] == 'Refer A Friend') {                                
    // Set the value of the hidden Refer a Friend field                                
    $form_state['values']['submitted']['referafriend_value'] = 'yes';                                
  }                                
}                                

现在剩下的就是更改新按钮中的文本。目前,它读取“请求更多信息。”使用一点点jQuery,我们可以将其更改为“推荐给朋友。”这不会改变表单的处理。Webform与单击哪个按钮有很大关系,这就是验证处理程序在条件中有一些奇怪的变量检查的原因。    

1
2
// See hook_form_alter in mymodule.module                                
$("#webform-client-form-36 #edit-refer-friend-btn").attr('value', 'Refer A Friend');                                

嗯,这个例子有点可怜,但效果很好。如果您有更好的解决方案,请向我提供解决方案的链接或说明。对于“两个提交按钮做同样的事情”问题,毫无疑问是一个独家php解决方案。    


来自  http://agileadam.com/2011/03/multiple-submit-buttons-on-a-webform/



Drupal 6: Multiple Submit buttons

This is a small piece of code that can be used when you have multiple submit buttons in the same form in Drupal. By using the “clicked_button” in submit function, we can redirect to a certain page based on the button clicked.

/*
 * Drupal form function
 */
function myform_form() {
    //button 1
    $form['heading'] = array (
        '#type' => 'markup',
        '#value' => t('Welcome to My FORM!!'),
    );
   $form['button_one'] = array (
        '#type' => 'fieldset',
        '#title' => 'BUTTON 1'
    );
    $form['button_one']['desc'] = array (
        '#type' => 'markup',
        '#value' => t('This is button 1 text'),
    );
     $form['button_one']['submit_one'] = array (
        '#value' => t('Button 1'),
        '#type' => 'submit',
    );

    //button 2
    $form['button_two'] = array (
        '#type' => 'fieldset',
        '#title' => 'BUTTON 2'
    );
    $form['button_two']['desc'] = array (
        '#type' => 'markup',
        '#value' => t('This is button 2 text'),
    );
     $form['button_two']['submit_two'] = array (
        '#value' => t('Button 2'),
        '#type' => 'submit',
    );

    return $form;
}

This is the submit function for the form

/*
 * Submit function
 * Use the clicked_button functionality
 */
function myform_form_submit($formID, &$form_state) {
    if($form_state['clicked_button']['#value'] == $form_state['values']['submit_one'])	  //if button 1 is clicked
        $form_state['redirect'] = 'mypath/page_one';	//redirect to whatever page you want
    else if($form_state['clicked_button']['#value'] == $form_state['values']['submit_two'])  //if button 2 is clicked
        $form_state['redirect'] = 'mypath/page_two';
}

Categories: Drupal


Comments

  1. <div class="comment-author author vcard"fonant="">

     February 22, 2013  12:45 pm

    If all your buttons have the same name (e.g. "op") then 'clicked_button' returns the last of them all the time. This can be fixed by setting the #names of the buttons explicitly, such as "op1", "op2", "op3", and so on. You have to start the #name with an alphabetical character, it can't be just a number.
  2. <div class="comment-author author vcard"Ændrew Rininsland (@aendrew)="">

     August 17, 2012  11:32 am

    Note that if you're wanting to have similar-looking sets of buttons, searching for value doesn't always work -- for instance, I had "move up" and "move down" buttons in each fieldset, and having "move up 1" and "move down 3" is confusing and doesn't make sense (Why didn't I use tabledrag or something instead, you ask? I was build a Panels content type admin form, which really don't play nicely with Javascript or anything remotely ajax-y, forcing me to use a purely FAPI implementation.). In such a case, you might want to set the '#name' attribute for each button and then test against that instead.
  3. <div class="comment-author author vcard"Sandeep="">

     June 6, 2012  6:22 am

    Thanks a lot..:)
  4. <div class="comment-author author vcard"web designer="">

     October 14, 2011  4:50 pm

    Just wanted to say thank you, this helped me as well with a form that has a dynamic number of buttons that redirect.
  5. <div class="comment-author author vcard"Chong="">

     May 7, 2011  8:41 am

    I google with multi submit and find this page, it helps me. Thanks.
  6. <div class="comment-author author vcard"Erik Beckers="">

     February 5, 2011  12:03 am

    Hi Naveen, your article helped me with my problem, my forms work now due to your hint. But: In Drupal 6.20 it is sufficient to check the content of $form_state['clicked_button']['#value'] in order to obtain the required button. A field set is not required. "clicked_button" is the key, thank you very much. Kind regards, Erik
    1. <div class="comment-author author vcard"Naveen Nayak="">

       February 7, 2011  8:28 pm

      Eric, i am glad that it helped you The field-set is there just for visual differentiation of the two buttons ( it has nothing to do with the submit function :) )

  7. 来自 https://naveensnayak.com/2010/03/09/drupal-multiple-submit-buttons/
普通分类: