Skip to main content

Submissions Queries

The Submissions query fetches a list of submissions based on some or no criteria.

Parameters

The following parameters are available for use:

id

An ID of a submission, or array of ID's, e.g. [54, 62, 68].

form

Handle of the form, e.g. "myForm".

formId

An ID of the form, e.g. 2.

limit

Supply an int to limit the amount of submissions returned.

orderBy

Use any field handle to order by that value and include the ASC or DESC parameter in the string, e.g. orderBy: "firstName ASC" or orderBy: "rand()".

status

Specify status to fetch submissions with a certain status, e.g .status: "open" if you have a status with a handle open.

token

Specify a token to more securely fetch a specific submission. A common use case would be when you want to display submission data in a success page, by loading the newly created token in the return URL. Can be used in conjunction with or in place of id.

Usage in Templates

Simple List

Display a simple list of submissions:

{% set submissions = freeform.submissions({
orderBy: "firstName ASC, lastName DESC",
status: ["pending", "closed"],
}) %}

<ul>
{% for submission in submissions %}
<li>{{ submission.title }} - {{ submission.firstName }}</li>
{% endfor %}
</ul>

Check if Fields Exist

Display all submissions and check if fields exist for the submitted form, before displaying them:

{% set submissions = freeform.submissions({
orderBy: "firstName ASC, lastName DESC",
status: ["pending", "closed"],
}) %}

{% for submission in submissions %}
<div>
<div>{{ submission.title }} - {{ submission.form.name }}</div>

{% if submission.firstName is not null %}
{{ submission.firstName.label }}: {{ submission.firstName }}<br>
{% endif %}

{% if submission.lastName is not null %}
{{ submission.lastName.label }}: {{ submission.lastName }}<br>
{% endif %}
</div>
{% endfor %}

Pagination

To paginate submissions, use Craft's Pagination. Here's an example:

{% paginate freeform.submissions({limit: 5}) as pageInfo, submissions %}

{% for submission in submissions %}
<div>
<div>{{ submission.title }} - {{ submission.form.name }}</div>
</div>
{% endfor %}

{% if pageInfo.prevUrl %}
<a href="{{ pageInfo.prevUrl }}">Previous Page</a>
{% endif %}
{% if pageInfo.nextUrl %}
<a href="{{ pageInfo.nextUrl }}">Next Page</a>
{% endif %}

Single Submission

To display a single submission (see Submission object for more info):

{% set submissionId = craft.app.request.segment(5) %}
{% set submission = freeform.submissions({id: submissionId}).one() %}

{% if submission %}
{% set form = submission.form %}

<h3>{{ form.name }} - {{ submission.title }}</h3>

<table class="table table-striped">
{% for field in submission %}
<tr>
<th style="width: 20%;">{{ field.label ? field.label : "no-label" }}</th>
<td>
{% set fieldValue = submission[field.handle].value %}
{% if fieldValue is iterable %}
<ul>
{% for value in fieldValue %}
<li>{{ value }}</li>
{% endfor %}
</ul>
{% else %}
{{ fieldValue }}
{% endif %}
</td>
</tr>
{% endfor %}
</table>

{% else %}

<div class="alert alert-danger">
<p class="lead">
Sorry, no submission was found.
</p>
</div>

{% endif %}

Display Uploaded Assets

The following is an example that shows how to render uploaded Assets in your form submissions:

{% set formHandle = 'yourFormHandle' %}
{% set submissions = freeform.submissions({
form: formHandle,
}) %}

<h3>Submissions for {{ form.name }}</h3>

{% if submissions is empty %}
<div>There are no submissions</div>
{% else %}
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Title</th>
{% for field in (submissions|first) %}
<th>{{ field.label }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for submission in submissions %}
<tr>
<td>{{ submission.id }}</td>
<td>
<a href="{{ siteUrl }}freeform_demo/bootstrap/{{ form.handle }}/submissions/{{ submission.id }}">
{{ submission.title }}
</a>
</td>
{% for field in submission %}
<td>
{% if field.type == "file" %}
{% set assetId = submission[field.handle] %}
{% set asset = craft.assets.id(assetId).one() %}
{% if asset %}
{% if asset.kind == "image" %}
<img src="{{ asset.url }}" />
{% else %}
<a href="{{ asset.url }}">{{ asset.filename }}</a>
{% endif %}
{% endif %}
{% else %}
{{ submission[field.handle] }}
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}