Documentation Docs
Documentation Docs

Store Survey Results

Survey results are JSON objects that you can store in your own database or in the SurveyJS Service.

Store Survey Results in Your Own Database

To store survey results in your own database, handle the onComplete event raised in response to a click on the Complete button. The following code shows how to use this event to send survey results to your server:

import { Model } from "survey-core";

const surveyJson = { ... };

const survey = new Model(surveyJson);
survey.onComplete.add(function (sender, options) {
  // Display the "Saving..." message (pass a string value to display a custom message)
  options.showSaveInProgress();
  const xhr = new XMLHttpRequest();
  xhr.open("POST", "your/server/url");
  xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  xhr.onload = xhr.onerror = function () {
    if (xhr.status == 200) {
      // Display the "Success" message (pass a string value to display a custom message)
      options.showSaveSuccess();
      // Alternatively, you can clear all messages:
      // options.clearSaveMessages();
    } else {
      // Display the "Error" message (pass a string value to display a custom message)
      options.showSaveError();
    }
  };
  xhr.send(JSON.stringify(sender.data));
});

The onComplete event handler only sends survey results in JSON format to your server. The way you store them fully depends on your backend.

If you are running a NodeJS server, you can check survey results before saving them. On the server, create a SurveyModel and call its clearIncorrectValues(true) method. This method verifies a survey result JSON object against the survey JSON schema and deletes property values that cannot be assigned to a question (such as choice options unlisted in a choices array) and property values that do not correspond to any question or calculated value.

// Server-side code for a NodeJS backend
import { Model } from "survey-core";

const surveyJson = { ... };
const survey = new Model(surveyJson);

survey.data = initialSurveyResultJson;
survey.clearIncorrectValues(true);

const correctSurveyResultJson = survey.data;

Store Survey Results in the SurveyJS Service

SurveyJS Service is a full-cycle survey solution. The service allows you to create a survey and store its JSON schema in our database. You can also load surveys from the database, display them to your clients, and send the results back to the service for storage and analysis.

SurveyJS Service is meant as a demonstration of what you can build with SurveyJS products. SurveyJS assumes no responsibility for any consequence of misusing or violating any sensitive data communicated via the service. In real-world applications, we strongly recommend storing survey results and JSON schemas in your own database.

Follow the steps below to start using the SurveyJS Service:

  1. Log in or register on the SurveyJS website.
  2. Create a new survey.
  3. Copy the Survey ID and Post ID: Survey ID and Post ID
  4. Assign the IDs to the surveyId and surveyPostId properties:
    const surveyJson = {
      "surveyId": "9b5f68e0-3be3-4695-85e6-919354159006",
      "surveyPostId": "dfce82b1-c081-45fe-b13e-fb2bd2977cc7"
    }
    
  5. (Optional) Enable the surveyShowDataSaving property to display saving progress and errors:
    const surveyJson = {
      // ...
      "surveyShowDataSaving": true
    }
    

View Demo

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.