Web3Forms
Search…
Ajax Contact Form using Javascript

HTML

1
<!--
2
=======================================================================
3
4
This is a working contact form. To receive email,
5
Replace YOUR_ACCESS_KEY_HERE with your actual Access Key.
6
7
Create Access Key here 👉 https://web3forms.com/
8
9
=======================================================================
10
-->
11
12
<div class="flex items-center min-h-screen bg-gray-50 dark:bg-gray-900">
13
<div class="container mx-auto">
14
<div class="max-w-md mx-auto my-10 bg-white p-5 rounded-md shadow-sm">
15
<div class="text-center">
16
<h1
17
class="my-3 text-3xl font-semibold text-gray-700 dark:text-gray-200"
18
>
19
Contact Us
20
</h1>
21
<p class="text-gray-400 dark:text-gray-400">
22
Fill up the form below to send us a message.
23
</p>
24
</div>
25
<div class="m-7">
26
<form action="https://api.web3forms.com/submit" method="POST" id="form">
27
<input type="hidden" name="apikey" value="YOUR_ACCESS_KEY_HERE" />
28
<input
29
type="hidden"
30
name="subject"
31
value="New Submission from Web3Forms"
32
/>
33
<input type="checkbox" name="botcheck" id="" style="display: none;" />
34
35
<div class="mb-6">
36
<label
37
for="name"
38
class="block mb-2 text-sm text-gray-600 dark:text-gray-400"
39
>Full Name</label
40
>
41
<input
42
type="text"
43
name="name"
44
id="name"
45
placeholder="John Doe"
46
required
47
class="w-full px-3 py-2 placeholder-gray-300 border border-gray-300 rounded-md focus:outline-none focus:ring focus:ring-indigo-100 focus:border-indigo-300 dark:bg-gray-700 dark:text-white dark:placeholder-gray-500 dark:border-gray-600 dark:focus:ring-gray-900 dark:focus:border-gray-500"
48
/>
49
</div>
50
<div class="mb-6">
51
<label
52
for="email"
53
class="block mb-2 text-sm text-gray-600 dark:text-gray-400"
54
>Email Address</label
55
>
56
<input
57
type="email"
58
name="email"
59
id="email"
60
placeholder="[email protected]"
61
required
62
class="w-full px-3 py-2 placeholder-gray-300 border border-gray-300 rounded-md focus:outline-none focus:ring focus:ring-indigo-100 focus:border-indigo-300 dark:bg-gray-700 dark:text-white dark:placeholder-gray-500 dark:border-gray-600 dark:focus:ring-gray-900 dark:focus:border-gray-500"
63
/>
64
</div>
65
<div class="mb-6">
66
<label for="phone" class="text-sm text-gray-600 dark:text-gray-400"
67
>Phone Number</label
68
>
69
<input
70
type="text"
71
name="phone"
72
id="phone"
73
placeholder="+1 (555) 1234-567"
74
required
75
class="w-full px-3 py-2 placeholder-gray-300 border border-gray-300 rounded-md focus:outline-none focus:ring focus:ring-indigo-100 focus:border-indigo-300 dark:bg-gray-700 dark:text-white dark:placeholder-gray-500 dark:border-gray-600 dark:focus:ring-gray-900 dark:focus:border-gray-500"
76
/>
77
</div>
78
<div class="mb-6">
79
<label
80
for="message"
81
class="block mb-2 text-sm text-gray-600 dark:text-gray-400"
82
>Your Message</label
83
>
84
85
<textarea
86
rows="5"
87
name="message"
88
id="message"
89
placeholder="Your Message"
90
class="w-full px-3 py-2 placeholder-gray-300 border border-gray-300 rounded-md focus:outline-none focus:ring focus:ring-indigo-100 focus:border-indigo-300 dark:bg-gray-700 dark:text-white dark:placeholder-gray-500 dark:border-gray-600 dark:focus:ring-gray-900 dark:focus:border-gray-500"
91
required
92
></textarea>
93
</div>
94
<div class="mb-6">
95
<button
96
type="submit"
97
class="w-full px-3 py-4 text-white bg-indigo-500 rounded-md focus:bg-indigo-600 focus:outline-none"
98
>
99
Send Message
100
</button>
101
</div>
102
<p class="text-base text-center text-gray-400" id="result"></p>
103
</form>
104
</div>
105
</div>
106
</div>
107
</div>
Copied!

Javascript

1
const form = document.getElementById("form");
2
const result = document.getElementById("result");
3
4
form.addEventListener("submit", function (e) {
5
const formData = new FormData(form);
6
e.preventDefault();
7
var object = {};
8
formData.forEach((value, key) => {
9
object[key] = value;
10
});
11
var json = JSON.stringify(object);
12
result.innerHTML = "Please wait...";
13
14
fetch("https://api.web3forms.com/submit", {
15
method: "POST",
16
headers: {
17
"Content-Type": "application/json",
18
Accept: "application/json",
19
},
20
body: json,
21
})
22
.then(async (response) => {
23
let json = await response.json();
24
if (response.status == 200) {
25
result.innerHTML = json.message;
26
result.classList.remove("text-gray-500");
27
result.classList.add("text-green-500");
28
} else {
29
console.log(response);
30
result.innerHTML = json.message;
31
result.classList.remove("text-gray-500");
32
result.classList.add("text-red-500");
33
}
34
})
35
.catch((error) => {
36
console.log(error);
37
result.innerHTML = "Something went wrong!";
38
})
39
.then(function () {
40
form.reset();
41
setTimeout(() => {
42
result.style.display = "none";
43
}, 5000);
44
});
45
});
Copied!
Last modified 8mo ago
Copy link