Webform

by Gisle Hannemyr

Webform is a module for making forms and surveys.

Table of contents

Drupal project discussed in this chapter: Webform, Webform MySQL Views.

To create a Webform, follow these steps:

  1. Choose Create content --- Webform. This opens the Create Webform form (see Figure 20-10). This is the Configuration tab, the first of two tabs you click in order to create your Web form.
  2. Fill in a Title for your form. The Description is optional.
  3. Enter text in the Confirmation message or redirect URL box. After your visitor fills out the form, this is the text he will see. If you prefer taking him to a Web page, enter the URL here instead of text.
  4. Scroll down to Webform mail settings to specify an email where the results of the form submission will be mailed.
    • You don't have to specify an email here to get the results of your form being submitted. After your form is set up, you can see the collected results of visitors submitting your form:
    • Choose Administer --- Content management --- Webforms. Click on the edit link next to the form you are interested in. Finally, click the Results tab to see a table of all the form submissions. Click on one to see the answers that user gave to the form questions.
  5. Click Save. You are now on the Form components setup page. This is where you add all the questions to your form. You can create text boxes, radio buttons, drop-down lists, and other standard form elements.
  6. Add a text box by typing a name for the field (for example, Last Name). Choose Textfield for type. Click the Add button.
  7. Add a drop-down box by typing a name for the field (for example, Favorite Color). Choose Select for Type. Click Add. Enter your choices, one per line, in the Options text box (for example, Red, Blue, Green). Click Submit.
  8. Click Publish to create your form.
  9. To view your new form, click on the View tab.
Figure 20-10: Webform configuration.

You still need to make your form available to people visiting your site. The easiest way to accomplish this is to edit your form and change the settings under the Menu setting. Select the menu where you want your form to appear, give it a title, and click Save.

Introduction

Webform is a module for using the GUI to make web forms (including wizards and surveys).

It is not meant to be a replacement for the core fields feature – where the administrator creates a content type form with identical fields that is populated by the user and where each filled in form becomes a full node.

Webform is designed to be used where each new webform node gets its own set of custom fields. The use case for the module is when you want your users (often anonymous) to be able to submit data that you want emailed to a few email addresses or saved to a CSV file.

You can even use it with fields to add additional fields to webform nodes!

Webform typically is used when performing data collection that is a one-way communication, that is, many users submitting values to a very small set of administrators.

A webform allow conditionals to appear in a form. Conditionals may be used to hide or show certain components (or entire pages) based on the value of other components.

After a submission customizable emails can be sent to administrators and/or submitters. Results can be exported into Excel or other spreadsheet applications. Webform also provides some basic statistical review and has an extensive API for expanding its features.

There exists additional modules to make use of the data generated by webform submissions. In Drupal 7, for example, you may use the modules Data and Webform MySQL Views to use Views to display and extract submissions (see Drupal.org. post Display Webform submission data in Views and a Vimeo screen­cast: Display Webform submission data in Views).

Installing and enabling the module

noteBy default the Webform content type do not have a body field. To have a body field, set the the configura­tion variable “webform_install_add_body_field” to TRUE before installing the module. You may do this using drush, by using the $conf[] array in settings.php or by making a helper project.

Install Webform as you would normally install a contributed drupal module.

The simplest way to do this is to use drush to both install and ebable:

$ sudo drush en webform -y

Now, the Webform module is set up and ready to use.

Creating a web form

To review the fields that the administrator may use to create a webform, navigate to Configuration » Content authoring » Webform settings. Note that these are similar to, but not the same as those used to put fields into nodes. For instance, the webform “Select options” field allows direct creation of checkboxes, radio buttons, and select menus, while the fields in core feature relies on taxonomy entities that is external to the field.

A webform is a Drupal content type. To manage its settings, navigate to Structure » Content types » Webform » edit. The defaults for “Publishing options” and “Display settings” usually need to be changed.

To create a webform node, navigate to Content » Add content » Webform.

Using webforms

The results of submissions to a particular webform can be viewed under the tab “Results”. To delete all results, press the button “Clear”. This operation cannot be undone.

Advanced form processing

The the “Form settings” tab provide limited options for setting the redirect location for the confirmation page. To redirect the confirmation based on the value entered in a Webform component, use hook_form_alter. The following implementation is used to redirect to a different ePay form depending on the type of registration selected in the webform.

/**
 * Implements hook_form_alter.
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ('webform_client_form_XX' == $form_id) {
    $form['#submit'][] = '_epay_redirect';
   }
}

function _epay_redirect(&$form, &$form_state) {
  $regtype = $form_state['values']['submitted'][1];
  switch ($regtype) {
    case 'fullconference':
      $form_state['redirect'] = 'https://epay.example.net/pay.html?regtype=…1';
      break;
    case 'daypass':
      $form_state['redirect'] = 'https://epay.example.net/pay.html?regtype=…2';
      break;
    default:
      drupal_set_message('Internal error, please contact webmasters.');
  }
}

Final word

This section is an introduction to this module. For more information about this robust module, I recommend the documentation at Drupal.org: Webform.

[TBA]


Last update: 2018-07-06 [gh].