-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathform.html
134 lines (122 loc) · 3.58 KB
/
form.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!--
Basic Form Example for testing
- Fields are tightly coupled with the API functionality
- Validation and error messages are returned from the server
- message: contains unordered list of issues
- fields: array of form ID selectors with issues
- JavaScript to handle basic form POST submission
-->
<form id="myForm" action="/v1/support" method="post">
<legend>Support Form</legend>
<p>
<label for="firstName">First Name</label>
<input id="firstName" name="firstName" type="text" />
</p>
<p>
<label for="lastName">Last Name</label>
<input id="lastName" name="lastName" type="text" />
</p>
<p>
<label for="email">Email *</label>
<input id="email" name="email" type="text" />
</p>
<p>
<label for="storeUrl">Store URL</label>
<input id="storeUrl" name="storeUrl" type="text" />
</p>
<p>
<label for="storePassword">Storefront Password</label>
<input id="storePassword" name="storePassword" type="text" />
</p>
<p>
<label for="theme">Theme</label>
<select id="theme" name="theme">
<option value="" disabled selected>Choose a theme</option>
<option value="District">District</option>
<option value="Laguna">Laguna</option>
</select>
</p>
<p>
<label for="subject">Subject</label>
<input id="subject" name="subject" type="text" />
</p>
<p>
<label for="message">Message *</label>
<textarea id="message" name="message"></textarea>
</p>
<p>
<label for="attachment">Attachement</label>
<input type="file" name="attachment" id="attachment" />
</p>
<p>
<button type="submit">Submit</button>
</p>
</form>
<div class="response"></div>
<style>
.hide {
display: none;
}
.response {
opacity: 0;
padding: 1em;
border-radius: 0.5em;
border: 4px solid rgba(200, 200, 200);
background: rgba(200, 200, 200, 0.2);
width: 100%;
max-width: 600px;
transition: all 400ms ease-in-out;
}
.response--success {
opacity: 1;
border: 4px solid green;
background: rgba(0, 128, 0, 0.2);
}
.response--error {
opacity: 1;
border: 4px solid red;
background: rgba(128, 0, 0, 0.2);
}
pre {
background: white;
padding: 1em;
border-radius: 0.5em;
}
</style>
<script>
window.addEventListener('load', function() {
function sendData() {
var XHR = new XMLHttpRequest();
var form = document.getElementById('myForm');
// Bind the FormData object and the form element
var FD = new FormData(form);
XHR.addEventListener('load', function(event) {
// Get the server response for success / error validation
var response = JSON.parse(event.target.responseText);
var responseBox = document.querySelector('.response');
responseBox.className = 'response';
// Handle responses
if (response.status === 'error') {
responseBox.classList.add('response--error');
} else {
// Success
responseBox.classList.add('response--success');
form.classList.add('hide');
}
// Pass response back to HTML for debugging
responseBox.innerHTML = `<h3>${response.message}</h3>
<pre>${JSON.stringify(response.data, null, 2)}</pre>`;
});
XHR.addEventListener('error', function(event) {
console.log('error', 'Oops! Something went wrong.');
});
XHR.open('POST', '/v1/support', true);
XHR.send(FD);
}
var form = document.getElementById('myForm');
form.addEventListener('submit', function(event) {
event.preventDefault();
sendData();
});
});
</script>