การรวมแบบฟอร์มการติดต่อ Astro (คงที่ & SSR)
วิธีเพิ่มแบบฟอร์มการติดต่อไปยัง Astro static sites (SSG) และเส้นทางเซิร์ฟเวอร์ไฮบริดโดยใช้ตำแหน่งข้อมูล Lugatu โดยไม่ต้องดูแลรักษาเซิร์ฟเวอร์อีเมลหรือฐานข้อมูล
1. การนำเข้าแบบฟอร์ม Pure Static Site (SSG)
Astro มีชื่อเสียงในด้าน Zero-JavaScript ตามค่าเริ่มต้น ด้วย Lugatu คุณสามารถทำให้ไซต์ Astro ของคุณคงที่ 100% ในขณะที่รวบรวมคำถามด้วยแบบฟอร์ม HTML มาตรฐานและการดึงข้อมูลจากฝั่งไคลเอ็นต์แบบน้ำหนักเบา
---
// src/pages/contact.astro
---
<form id="astro-contact-form" class="space-y-4 max-w-md">
<div>
<label class="block text-sm font-semibold mb-1">Your Name</label>
<input type="text" name="name" required class="w-full border rounded-xl p-2.5" />
</div>
<div>
<label class="block text-sm font-semibold mb-1">Email Address</label>
<input type="email" name="email" required class="w-full border rounded-xl p-2.5" />
</div>
<div>
<label class="block text-sm font-semibold mb-1">Message</label>
<textarea name="message" rows="4" required class="w-full border rounded-xl p-2.5"></textarea>
</div>
<input type="text" name="_gotcha" style="display:none !important" tabindex="-1" autocomplete="off" />
<button type="submit" class="btn-primary-editorial py-2.5 px-6">Send Message</button>
</form>
<script>
const form = document.getElementById('astro-contact-form') as HTMLFormElement;
form?.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(form);
const payload = {
fields: {
name: formData.get('name'),
email: formData.get('email'),
message: formData.get('message'),
},
honeypot: formData.get('_gotcha') || '',
};
const res = await fetch('https://www.lugatu.com/api/v1/public/forms/YOUR_PUBLIC_FORM_ID/submissions', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
body: JSON.stringify(payload),
});
if (res.ok) {
alert('Thank you! Message delivered.');
form.reset();
}
});
</script>
2. เส้นทาง Astro Hybrid / SSR API พร้อมคีย์ Server API
หากไซต์ Astro ของคุณทำงานในโหมดไฮบริดหรือ SSR บน Cloudflare Pages, Netlify หรือ Vercel ให้ส่งต่อการส่งอย่างปลอดภัยผ่านเส้นทาง Astro API
import type { APIRoute } from 'astro';
export const POST: APIRoute = async ({ request }) => {
const data = await request.formData();
const fields = {
name: data.get('name')?.toString(),
email: data.get('email')?.toString(),
message: data.get('message')?.toString(),
};
const res = await fetch(`https://www.lugatu.com/api/v1/forms/${import.meta.env.LUGATU_FORM_ID}/submissions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${import.meta.env.LUGATU_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': crypto.randomUUID(),
},
body: JSON.stringify({ fields }),
});
return new Response(JSON.stringify({ success: res.ok }), {
status: res.status,
headers: { 'Content-Type': 'application/json' },
});
};
ความปลอดภัยและการรับรองความถูกต้องไม่แปรผัน
คำแนะนำด้านความปลอดภัย: อย่าเปิดเผย import.meta.env.LUGATU_API_KEY ในแท็กสคริปต์ฝั่งไคลเอ็นต์ Astro สคริปต์ไคลเอนต์ต้องใช้ปลายทางสาธารณะเท่านั้น
การแก้ไขปัญหาและข้อผิดพลาดในการนำเข้าทั่วไป
ตรวจสอบให้แน่ใจว่าตัวแปรสภาพแวดล้อมการใช้งานจริงของคุณ (LUGATU_FORM_ID และ LUGATU_API_KEY) ได้รับการกำหนดค่าในแดชบอร์ดโฮสติ้งของคุณ
คำถามที่พบบ่อย
ฉันสามารถทำให้เว็บไซต์ Astro ของฉันคงที่ 100% กับ Lugatu ได้หรือไม่?
ใช่. ไซต์ Pure Static (SSG) ส่งโดยตรงไปยังปลายทางสาธารณะ Lugatu โดยไม่ต้องใช้เซิร์ฟเวอร์ประมวลผลบนผู้ให้บริการโฮสติ้งของคุณ
พร้อมที่จะสร้างจุดสิ้นสุดของแบบฟอร์มแล้วหรือยัง?
เริ่มต้นฟรีในไม่กี่วินาที ไม่ต้องใช้บัตรเครดิต