How to create Quiz in modx

515 Views Asked by At

I am new to modx and I have to create multipage quiz in my website. Any suggestion will be helpfull.

1

There are 1 best solutions below

0
On

While there are more than one way to achieve this, a combination of FormIt and FormItRetriever extras might just be what you need. FormIt handles the processing of your quiz forms, which includes saving the data in the cache or on the database as a JSON object. And, as the name suggests, FormItRetriever allows you to retrieve previously saved form data on a subsequent page.

Here's a quick example adapted from FormIt's docs:

Page 1

[[!FormIt?
 &submitVar=`go`
 &hooks=`spam,redirect`
 &store=`1`
 &redirectTo=`id-of-next-page`
]]
<form action="[[~[[*id]]]]" method="post">
    <input type="hidden" name="nospam" value="" />
    <label for="qzq1">Quiz question 1: [[!+fi.error.qzq1]]</label>
    <input type="text" name="qzq1:required" id="qzq1" value="[[!+fi.qzq1]]" />
    <label for="qzq2">Quiz question 2: [[!+fi.error.qzq2]]</label>
    <input type="text" name="qzq2:required" id="qzq2" value="[[!+fi.qzq2]]" />
    <label for="qzq3">Quiz question 3: [[!+fi.error.qzq3]]</label>
    <textarea name="qzq3:stripTags" id="qzq3" cols="55" rows="7">[[!+fi.qzq3]]</textarea>
    <br />
    <input type="submit" name="go" value="Next" />
</form>

The &store property tells FormIt to store the data in the cache for retrieval using the FormItRetriever snippet.

The &redirectTo property is the ID of your next page. FormIt will use the redirect hook, specified in the &hooks property, to redirect the user when they submit this form.

Page 2:

[[!FormItRetriever]]

[[!FormIt?
 &submitVar=`go`
 &hooks=`spam,redirect`
 &store=`1`
 &redirectTo=`id-of-third-page`
]]

/* Page 2 quiz form goes here */

The FormItRetriever snippet will allow you to display your previously saved form data with placeholders relating to the names of your form fields => [!+fi.qzq1]]

To store the quiz form data on the database, you can use FormItSaveForm. This allows you to later view the data inside a Custom Manager Page (CMP) and export it, if need be.

Refer to the official docs for more usage examples: https://docs.modx.com/extras/revo/formit