-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.js
59 lines (52 loc) · 2.32 KB
/
form.js
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
"use strict";
document.getElementById("customFurnitureForm").addEventListener("submit", async function (event) {
event.preventDefault(); // Prevent the form from reloading the page
// Collect form data
const name = document.getElementById("name").value.trim();
const email = document.getElementById("email").value.trim();
const furnitureType = document.getElementById("furnitureType").value;
const dimensions = document.getElementById("dimensions").value.trim();
const material = document.getElementById("material").value.trim();
const budget = document.getElementById("budget").value.trim();
const phone = document.getElementById("phone").value.trim();
const notes = document.getElementById("notes").value.trim();
// Airtable API configuration
const apiKey = "Bearer patLq2nVN05VN1onU.0d2bee27d84384c7dc954b7ebac6a193a79c181704b2d45752159203feaf8348";
const baseId = "YOUR_AIRTABLE_BASE_ID"; // Replace with your Airtable Base ID
const tableName = "Table 1"; // Use the name of your Airtable table (e.g., "Table 1")
const url = `https://api.airtable.com/v0/${baseId}/${encodeURIComponent(tableName)}`;
// Prepare data to send to Airtable
const data = {
fields: {
Name: name,
Email: email,
"Furniture Type": furnitureType,
Dimensions: dimensions,
"Material Preference": material,
"Budget Range": parseFloat(budget),
"Phone Number": phone,
"Additional Notes": notes,
},
};
try {
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (response.ok) {
alert("Your request has been submitted successfully!");
document.getElementById("customFurnitureForm").reset(); // Clear the form
} else {
const errorData = await response.json();
console.error("Error:", errorData);
alert("There was an error submitting your request. Please try again.");
}
} catch (error) {
console.error("Error:", error);
alert("An unexpected error occurred. Please try again.");
}
});