Use Variables
Easily extend your Slapforms with variables to insert dynamic data and calculated fields.
Understand variables
Variables let you insert dynamic content into your forms.
Variables look like a snippet of text wrapped in special characters: ${example}. When you insert a variable into your submission, we'll replace it with the value stored in the corresponding variable.
Access system variables
System variables are predefined and every form/submission has access to them.
${ slap_meta_date }
| Name | ${ slap_meta_date } |
|---|---|
| Example value | 2024-01-15T10:30:00.000Z |
| Description | The current date and time in yyyy-mm-ddThh:mm:ss.fffZ format. |
${ slap_meta_referrer }
| Name | ${ slap_meta_referrer } |
|---|---|
| Example value | https://yourwebsite.com |
| Description | The referring URL that the form came from. For AJAX forms, this will be undefined. |
${ slap_meta_submission }
| Name | ${ slap_meta_submission } |
|---|---|
| Example value | 1 |
| Description | The number of the current submission for the current submission period. |
${ slap_meta_status }
| Name | ${ slap_meta_status } |
|---|---|
| Example value | success |
| Description | The status of the submission. Can be either success or fail. |
Define submission variables
Submission variables are defined by you, the developer.
Simply enclose the name of any field in the Slapform variable syntax from within another field and the data will automatically be inserted.
For example:
Input
{
"variable": "apples",
"result": "He likes ${variable}."
}Result
{
"variable": "apples",
"result": "He likes apples."
}Try the HTML form example with variables
Below is an example of an HTML form utilizing some variables.
<form method="POST"
action="https://api.slapform.com/{form_id}">
<input type="text" name="name">
<input type="text" name="slap_subject" value="New message from ${name} on ${slap_meta_date}" hidden>
<textarea type="text" name="message"></textarea>
<button type="submit">Submit</button>
</form>So what's happening here? There are two variables in use here, ${name} and ${slap_meta_date}.
The variable ${name} would be replaced by the contents of the field with the name attribute name, and the variable ${slap_meta_date} would be replaced by the current date and time.
Pro Tip
Generally, you should hide the fields that use variables because it is unreasonable to expect your users to fill out those fields.
Try the JavaScript form example with variables
Below is an example of a JavaScript form utilizing some variables.
slapform.submit({
form: '{form_id}',
data: {
name: 'Jon Snow',
message: 'Hello World! This is my first Slapform submission.',
slap_subject: 'New message from ${name} on ${slap_meta_date}'
}
})
.then(function (response) {
console.log('Success!', response);
})
.catch(function (response) {
console.log('Fail!', response);
})
.finally(function () {
console.log('This always runs!');
});