Examples
This guide outlines various methods for submitting the form.
You can access your form's endpoint by navigating to the following path:
dashboard -> Forms -> "Any Form" -> Setup.
HTML
After clicking Submit the user will be redirected to the default "Thank You" page or a custom URL if configured.
For a detailed overview of potential responses from your form's endpoint, refer to the documentation here.
enctype="multipart/form-data" in your form element.<form action="{your_form_endpoint}" method="POST" enctype="multipart/form-data">
<label>
Email:
<input type="email" name="email" />
</label>
<label>
Name:
<input type="text" name="name" />
</label>
<label>
Message:
<textarea name="message"></textarea>
</label>
<label>
File:
<input type="file" name="file" />
</label>
<button type="submit">Submit</button>
</form>JSON using JavaScript
For a detailed overview of potential responses from your form's endpoint, refer to the documentation here.
const data = {
email: "test@vexrun.com",
name: "John Doe",
message: "Hello!",
};
const res = await fetch("{your_form_endpoint}", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
const json = await res.json();
console.log(json);FormData using JavaScript
For a detailed overview of potential responses from your form's endpoint, refer to the documentation here.
const data = new FormData();
data.append("email", "test@vexrun.com");
data.append("name", "John Doe");
data.append("message", "Hello!");
const file = document.querySelector("input[name=file]").files[0];
data.append("file", file);
const res = await fetch("{your_form_endpoint}", {
method: "POST",
headers: {
Accept: "application/json",
},
body: data,
});
const json = await res.json();
console.log(json);Validation
This example demonstrates how to use built-in browser validation and JavaScript to ensure data integrity before submitting a form.
Used APIs that you might not be familiar with:
- form element's novalidate attribute
- HTMLFormElement.reportValidity()
- HTMLInputElement.setCustomValidity()
- FormData
<form id="my-form" action="{your_form_endpoint}" method="POST" enctype="multipart/form-data" novalidate>
<label>
Email:
<input type="email" name="email" required maxlength="200" />
</label>
<label>
Name:
<input type="text" name="name" required minlength="3" maxlength="50" />
</label>
<label>
Message:
<textarea name="message" required maxlength="5000"></textarea>
</label>
<label>
File:
<input id="my-file-field" type="file" name="file" required />
</label>
<button type="submit">Submit</button>
</form>const maxFileSize = 10_000_000; // 10 MB
const form = document.querySelector("#my-form");
const fileField = document.querySelector("#my-file-field");
form.addEventListener("submit", async (event) => {
// Prevents the default form submission behavior
event.preventDefault();
const file = fileField.files?.[0];
if (file && file.size > maxFileSize) {
fileField.setCustomValidity(`File is larger than ${maxFileSize / 1_000_000} MB.`);
} else {
fileField.setCustomValidity("");
}
// form.reportValidity will validate all inputs based on their
// attributes (e.g. required, minlength). Unfortunately it doesn't
// support validating file size, hence the above code.
const isValid = form.reportValidity();
if (isValid === false) {
return;
}
// All fields are valid
const data = new FormData(form);
const res = await fetch(form.action, {
method: "POST",
headers: {
Accept: "application/json",
},
body: data,
});
console.log(await res.json());
});