-2

I have a webform but it's very long and users are going to use it a lot of times so they requested to have sort of like a cookie storage that remembers submitted data on some fields. For example I have a country field country: text_field (user inputs something).

I want to collect that country input from that user so when the next time user visits this form it will already have that country input in place so the users dont need to re-type it. I thought about having a cookie to store this but it feel likes it should be done in a different way.

Suggestions?

1

The webform module has the options to store incomplete webforms. You can enable the option 'Show "save draft" button' (this can be found on the advanced fieldset of the webform config). I haven't completely tested it but my best guess is that it only works for authenticated users.

When the user saves the webform and comes back to the webform, the supplied values are filled and the user can continue with the webform. It's olso possible to make the webform a multistep webform. When a users is going to a next step, webform saves the previous step. This also allows the user to continue where it left off.

I'm currently using Webform 7.x-3.18 for the features described above. I'm using a multi step form. At each stap the submitted values are saved as a draft.

0

There could be two cases either registered user or anonymous.

If user registered then:

  1. create a custom module let called "MYMODULE".
  2. In MYMODULE.module file do the following:

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, $form_state, $form_id) {

  switch ($form_id) {
    case 'your_webform_id':
      if (user_is_logged_in()) {

        global $user;

        // Select data from table as you need.
        $query = db_select('webform_submissions', 'ws');
        $query->leftJoin('webform_submitted_data', 'wsd', 'body.entity_id = n.nid');
        $query->fields('ws', ['nid', 'uid'])
          ->fields('wsd', ['data']);
        $query->condition('ws.uid', $user->uid);

        $result = $query->execute();

        while ($record = $result->fetchAssoc()) {

          // Set your $form field #default attribute here.
        }
      }
      else {

        // If user not registered yet and have data in COOKIE.
        if (isset($_COOKIE['webform_id']['field_name'])) {

          // Set your $form field #default attribute here.
        }
      }
      break;
  }
}

/**
 * Call submit hook here.
 */
function MYMODULE_webform_id_submit($form, &$form_state) {

  if (!user_is_logged_in()) {
    $COOKIE['webform_id']['field_name'] = $form_state['values']['field_name']; // set your cookie here
  }
}

NOTE:- please note above code is just an example and not tested code. please modify as per your requirement. above code is just for explaining purpose.

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.