SurveyJS PHP Backend Integration with Laravel /

Load JSON Schemas, Variables, and Answers

Load JSON Schemas, Variables, and Answers

Use when: Your form needs user-specific data or previously saved answers.

Read this step in the docs

Try this

  1. Option A (/claim): the title says "Hi there" and Requests shows no JSON schema request.
  2. Option B (this page): one GET /api/forms/claim-request?record= returns the JSON schema, variables, and previous answers. Open Requests and expand it to see them.
  3. In Option B, the title shows Alice's name and premium plan, and "use priority handling?" shows because Alice is on the premium plan.
  4. Pick a Record: the form opens pre-filled and edits that claim instead of starting blank.
  5. Press Complete: Data shows responses with the answers and no variables. Press Start over to fill it out again.

    No requests yet.

      What the server stored
      Option A: rendered into the page B: fetched

      Source on GitHub Definition: shared/definitions/load-definition-variables-data.json

      Client · shared/client/load-definition-variables-data.js · sjs:load-definition-variables-data.client View on GitHub
      import { Model } from "survey-core";
      
      async function createSurvey(recordId) {
        if (window.FORM_DEFINITION) {
          // Option A — your server rendered the definition into the page
          return new Model(window.FORM_DEFINITION);
        }
        // Option B — one request returns the definition, variables and previous answers
        const { definition, variables, data } =
          await fetch(`/api/forms/claim-request?record=${recordId}`).then(r => r.json());
        const survey = new Model(definition);
        survey.setVariables(variables);   // all at once; read as {user.name}, {plan}… and never saved with the response
        survey.data = data;               // previous answers: edit instead of starting blank
        return survey;
      }
      Server · routes/examples/load-definition-variables-data.php · sjs:load-definition-variables-data.server View on GitHub
      // Option A — render the definition into the page: no database, no extra request
      Route::get('/claim', function () {
          $definition = json_decode(file_get_contents(base_path('shared/definitions/load-definition-variables-data.json')));
          $definition->title = 'Hi '.Str::before(Auth::user()->name ?? 'there', ' ');   // change it per request if you like
      
          return view('examples.load-definition-variables-data', ['definition' => $definition]);   // Js::from() escapes it for an inline script
      });
      
      // Option B — definition, variables and previous answers in one response
      Route::get('/api/forms/{id}', function (Request $request, string $id) {
          $user = Auth::user();
          $definition = DB::table('forms')->where('key', $id)->value('json') ?? abort(404, 'Unknown form');
          $data = $request->filled('record') ? DB::table('responses')->where('id', $request->query('record'))->value('data') : null;
      
          return StoredJson::response([
              'definition' => json_decode($definition),                                // a stored document, as stored
              'variables' => ['user' => ['name' => $user?->name, 'plan' => $user?->plan]],
              'data' => $data === null ? new stdClass : json_decode($data),           // previous answers, or {}
          ]);
      });