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
.
To save incomplete results, implement a function that sends them to your server or saves them in the localStorage
. Call this function within SurveyModel
's onValueChanged
and onCurrentPageChanged
event handlers to save survey results when users change a question value or switch between pages. If you use the localStorage
, you also need to delete survey results from it when the survey is completed. Handle the onComplete
event for this purpose.
import { Model } from "survey-core";
const surveyJson = { ... };
const survey = new Model(surveyJson);
const storageItemKey = "my-survey";
function saveSurveyData (survey) {
const data = survey.data;
data.pageNo = survey.currentPageNo;
window.localStorage.setItem(storageItemKey, JSON.stringify(data));
}
// Save survey results to the local storage
survey.onValueChanged.add(saveSurveyData);
survey.onCurrentPageChanged.add(saveSurveyData);
// Restore survey results
const prevData = window.localStorage.getItem(storageItemKey) || null;
if (prevData) {
const data = JSON.parse(prevData);
survey.data = data;
if (data.pageNo) {
survey.currentPageNo = data.pageNo;
}
}
// Empty the local storage after the survey is completed
survey.onComplete.add(() => {
window.localStorage.setItem(storageItemKey, "");
});