DataJet
Search…
Processing user submitted forms
Let's assume you want to give your customers an option to submit a form. Following submission form will be processed and a notification send to specified email address.
1
<form id="data" method="post" enctype="multipart/form-data" action="/apps/datajet/task/[your_task_id]">
2
<input type="text" name="first" value="">
3
<input type="text" name="last" value="">
4
<input type="file" name="photo" >
5
<button>Submit</button>
6
</form>
7
<script>
8
$("form#data").submit(function(e) {
9
e.preventDefault();
10
var formData = new FormData(this);
11
12
$.ajax({
13
url: $(this).attr("action"),
14
type: 'POST',
15
data: formData,
16
success: function (data) {
17
alert(data)
18
},
19
mimeType: "multipart/form-data",
20
contentType: false,
21
processData: false
22
});
23
});
24
</script>
Copied!
Above code creates a simple form with 2 text input fields and one file input field. This form is submitted via Ajax call to DataJet endpoint (please note task id is missing, replace it with your own task id).
Next, lets create a HTTP task with following code:
1
{% assign form_first_name = request.body.first | default: "" %}
2
{% assign form_last_name = request.body.last | default: "" %}
3
4
{% json upload_options %}
5
{
6
"fileName": "{{request.files.photo.name}}",
7
"multipart": {{request.files.photo | default: "" | json }},
8
"public": true
9
}
10
{% endjson %}
11
12
{% assign result = upload_options | file %}
13
14
{% assign file_url = result.url %}
15
16
{% capture email_html %}
17
<div>
18
<h2> Hello! </h2>
19
<p> You have new form submission from {{form_first_name}} {{form_last_name}} </p>
20
<p> Following photo was attached: </p>
21
<img src="{{file_url}}" width="500" height="600">
22
</div>
23
{% endcapture %}
24
25
{% capture email_params %}
26
{
27
28
"subject": "New Form Submission",
29
"replyTo": "[email protected]",
30
"html": {{ email_html | json }}
31
}
32
{% endcapture %}
33
34
{{ email_params | email }}
35
36
{% json res %}
37
{"status": "ok"}
38
{% endjson %}
39
40
{% assign response = res %}
Copied!
This code:
  1. 1.
    Accepts form submission
  2. 2.
    Uploads attached file to your local storage
  3. 3.
    Prepares html template for email
  4. 4.
    Sends an email to address specified
Last modified 4mo ago
Copy link