Documentation Docs
Documentation Docs

Continue an Incomplete Survey

Respondents may not complete your survey in a single session. In this case, you can restore their answers from the previous session next time they get to the survey. Incomplete results can be loaded from your database or the browser's localStorage.

Restore Survey Progress from the localStorage

To save incomplete survey results locally, implement a function that stores them under a specified key in the localStorage (see the saveSurveyData function in the code below). Call this function within SurveyModel's onValueChanged and onCurrentPageChanged event handlers to save the survey results each time users change a question value or switch between pages. When the survey is completed, submit final survey results to the server using the onComplete event handler:

import { Model } from "survey-core";

const survey = new Model();

const STORAGE_ITEM_KEY = "my-survey";
const SURVEY_ID = /* ... Getting the survey ID ... */;
const ENDPOINT_URL = "https://example.com/api/responses/" + SURVEY_ID;

fetch("https://example.com/api/surveys/" + SURVEY_ID)
  .then(response => response.json())
  .then(loadedSurvey => {
    survey.fromJSON(loadedSurvey.json);
    restoreSurveyData(survey);
  })
  .catch(error => console.error(error));

function saveSurveyData (survey) {
  const data = survey.data;
  data.pageNo = survey.currentPageNo;
  window.localStorage.setItem(STORAGE_ITEM_KEY, JSON.stringify(data));
}

function submitSurveyData (data) {
  fetch(ENDPOINT_URL, {
    method: 'POST',
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(data)
  })
    .then(response => response.json())
    .then(data => {
      console.log(data);
      window.localStorage.setItem(STORAGE_ITEM_KEY, "");
    })
    .catch(error => console.error(error));
}

function restoreSurveyData (survey) {
  const prevData = window.localStorage.getItem(STORAGE_ITEM_KEY) || null;
  if (prevData) {
    const data = JSON.parse(prevData);
    survey.data = data;
    if (data.pageNo) {
      survey.currentPageNo = data.pageNo;
    }
  }
}

// Save survey results when users change a question value...
survey.onValueChanged.add(saveSurveyData);
// ... and switch to the next page
survey.onCurrentPageChanged.add(saveSurveyData);

// Submit final survey results after the survey is completed
survey.onComplete.add((survey) => {
  const userId = /* ... Getting the user ID ... */
  survey.setValue("userId", userId);
  submitSurveyData(survey.data);
});

View Demo

Restore Survey Progress from a Database

To save incomplete results in a database, submit them to your server each time users change a question value or switch between pages and when they complete the survey. Handle the onValueChanged, onCurrentPageChanged, and onComplete events for this purpose.

import { Model } from "survey-core";

const survey = new Model();

const SURVEY_ID = /* ... Getting the survey ID ... */
const USER_ID = /* ... Getting the user ID ... */
const ENDPOINT_URL = "https://example.com/api/responses/" + SURVEY_ID + "/" + USER_ID;

fetch("https://example.com/api/surveys/" + SURVEY_ID)
  .then(response => response.json())
  .then(loadedSurvey => {
    survey.fromJSON(loadedSurvey.json);
    restoreSurveyData(survey);
  })
  .catch(error => console.error(error));

function saveSurveyData (survey) {
  const data = survey.data;
  data.pageNo = survey.currentPageNo;
  submitSurveyData(data);
}

function submitSurveyData (data) {
  fetch(url, {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(data)
  })
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error(error));
}

function restoreSurveyData (survey) {
  fetch(ENDPOINT_URL)
    .then(response => response.json())
    .then(prevData => {
      if (prevData) {
        const data = JSON.parse(prevData);
        survey.data = data;
        if (data.pageNo) {
          survey.currentPageNo = data.pageNo;
        }
      }
    })
    .catch(error => console.error(error));
}

// Submit survey results when users change a question value...
survey.onValueChanged.add(saveSurveyData);
// ... switch to the next page...
survey.onCurrentPageChanged.add(saveSurveyData);
// ... and complete the survey
survey.onComplete.add(saveSurveyData);

See Also

Send feedback to the SurveyJS team

Need help? Visit our support page

Copyright © 2024 Devsoft Baltic OÜ. All rights reserved.

Your cookie settings

We use cookies on our site to make your browsing experience more convenient and personal. In some cases, they are essential to making the site work properly. By clicking "Accept All", you consent to the use of all cookies in accordance with our Terms of Use & Privacy Statement. However, you may visit "Cookie settings" to provide a controlled consent.

Your renewal subscription expires soon.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.

Your renewal subscription has expired.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.