Spaces:
Running
Running
Ig0tU commited on
Commit ·
7315c4c
1
Parent(s): 42d4b91
Enhance dynamic integration script to map any form fields.
Browse files- public/live-site-integration.js +58 -17
- server.js +3 -2
public/live-site-integration.js
CHANGED
|
@@ -48,26 +48,67 @@
|
|
| 48 |
// Based on your HTML: id="name", id="email", id="phone", id="subject"
|
| 49 |
// We use document.getElementById or form.querySelector to be safe.
|
| 50 |
|
| 51 |
-
|
| 52 |
-
const getEmail = () => form.querySelector('[name="email"]')?.value || '';
|
| 53 |
-
const getPhone = () => form.querySelector('[name="phone"]')?.value || '';
|
| 54 |
-
const getSubject = () => form.querySelector('[name="subject"]')?.value || '';
|
| 55 |
-
|
| 56 |
-
// For the message, usually it's a textarea named 'message' or 'comments'
|
| 57 |
-
const getMessage = () => {
|
| 58 |
-
const msgEl = form.querySelector('textarea[name="message"], textarea[name="comments"]');
|
| 59 |
-
return msgEl ? msgEl.value : 'No message provided.';
|
| 60 |
-
};
|
| 61 |
-
|
| 62 |
const formData = {
|
| 63 |
-
name:
|
| 64 |
-
email:
|
| 65 |
-
phone:
|
| 66 |
-
subject:
|
| 67 |
-
message:
|
| 68 |
-
origin: 'Live Website: ' + window.location.hostname
|
| 69 |
};
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
try {
|
| 72 |
const response = await fetch(API_URL, {
|
| 73 |
method: 'POST',
|
|
|
|
| 48 |
// Based on your HTML: id="name", id="email", id="phone", id="subject"
|
| 49 |
// We use document.getElementById or form.querySelector to be safe.
|
| 50 |
|
| 51 |
+
// Gather data dynamically from any form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
const formData = {
|
| 53 |
+
name: '',
|
| 54 |
+
email: '',
|
| 55 |
+
phone: '',
|
| 56 |
+
subject: document.title || 'Website Form Submission',
|
| 57 |
+
message: '',
|
| 58 |
+
origin: 'Live Website: ' + window.location.hostname + window.location.pathname
|
| 59 |
};
|
| 60 |
|
| 61 |
+
let extraFields = [];
|
| 62 |
+
let hasMessage = false;
|
| 63 |
+
|
| 64 |
+
// Loop through all form elements
|
| 65 |
+
Array.from(form.elements).forEach(el => {
|
| 66 |
+
if (!el.name || el.type === 'submit' || el.type === 'button') return;
|
| 67 |
+
|
| 68 |
+
const name = el.name.toLowerCase();
|
| 69 |
+
const value = el.value.trim();
|
| 70 |
+
const label = (el.labels && el.labels.length > 0) ? el.labels[0].innerText.replace('*', '').trim() : el.name;
|
| 71 |
+
|
| 72 |
+
if (!value) return;
|
| 73 |
+
|
| 74 |
+
// Try to map obvious fields
|
| 75 |
+
if ((name.includes('name') || name.includes('first') || name.includes('last')) && !formData.name) {
|
| 76 |
+
formData.name = value;
|
| 77 |
+
} else if (name.includes('email') && !formData.email) {
|
| 78 |
+
formData.email = value;
|
| 79 |
+
} else if ((name.includes('phone') || name.includes('tel') || el.type === 'tel') && !formData.phone) {
|
| 80 |
+
formData.phone = value;
|
| 81 |
+
} else if (name.includes('subject')) {
|
| 82 |
+
formData.subject = value;
|
| 83 |
+
} else if ((name.includes('message') || name.includes('comments') || el.tagName.toLowerCase() === 'textarea') && !hasMessage) {
|
| 84 |
+
formData.message = value;
|
| 85 |
+
hasMessage = true;
|
| 86 |
+
} else {
|
| 87 |
+
// Unmatched field, collect it
|
| 88 |
+
extraFields.push(`${label}: ${value}`);
|
| 89 |
+
}
|
| 90 |
+
});
|
| 91 |
+
|
| 92 |
+
// Set fallbacks for strict checking on the backend
|
| 93 |
+
if (!formData.name && !formData.email && !formData.phone) {
|
| 94 |
+
formData.name = "Anonymous User";
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
if (!formData.message && extraFields.length === 0) {
|
| 98 |
+
formData.message = "No message body provided.";
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
// If we didn't explicitly find a 'message' box, but we have extra fields,
|
| 102 |
+
// or if we have both, combine them into the final message column.
|
| 103 |
+
if (extraFields.length > 0) {
|
| 104 |
+
const extras = extraFields.join('\\n');
|
| 105 |
+
if (hasMessage) {
|
| 106 |
+
formData.message += `\\n\\n--- Additional Form Data ---\\n${extras}`;
|
| 107 |
+
} else {
|
| 108 |
+
formData.message = `--- Form Submission Data ---\\n${extras}`;
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
try {
|
| 113 |
const response = await fetch(API_URL, {
|
| 114 |
method: 'POST',
|
server.js
CHANGED
|
@@ -61,8 +61,9 @@ app.post('/api/submit', async (req, res) => {
|
|
| 61 |
try {
|
| 62 |
const { name, email, message, phone, subject, origin } = req.body;
|
| 63 |
|
| 64 |
-
if (!
|
| 65 |
-
|
|
|
|
| 66 |
}
|
| 67 |
|
| 68 |
if (!spreadsheetId || spreadsheetId === 'your_google_sheet_id_here') {
|
|
|
|
| 61 |
try {
|
| 62 |
const { name, email, message, phone, subject, origin } = req.body;
|
| 63 |
|
| 64 |
+
if (!email && !phone && !name) {
|
| 65 |
+
// We need at least ONE piece of identifying info or data to consider it valid
|
| 66 |
+
return res.status(400).json({ error: 'Please provide at least a name, email, or phone number.' });
|
| 67 |
}
|
| 68 |
|
| 69 |
if (!spreadsheetId || spreadsheetId === 'your_google_sheet_id_here') {
|