Skip to content

Commit 74916e9

Browse files
committed
update leadform
1 parent 757f4aa commit 74916e9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/pages/api/leadform.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export const prerender = false; //This will not work without this line
2+
3+
import type { APIRoute, APIContext } from 'astro';
4+
5+
export const POST: APIRoute = async ({ request, locals}: APIContext) => {
6+
const data = await request.formData();
7+
const name = data.get("usrname");
8+
const email = data.get("email");
9+
const message = data.get("msg");
10+
const refer = data.get("ref");
11+
12+
if (!locals || !locals.runtime || !locals.runtime.env || !locals.runtime.env.DB) {
13+
return new Response('Database not configured', { status: 500 });
14+
}
15+
16+
const { DB } = locals.runtime.env;
17+
18+
if (!name || !email || !message || !refer) {
19+
return new Response('Missing required fields', { status: 400 });
20+
}
21+
22+
// Call a function to save the email to the D1 database
23+
const query = 'INSERT INTO leads (name, email, refer, message, timestamp) VALUES (?1, ?2, ?3, ?4, CURRENT_TIMESTAMP)';
24+
await DB.prepare(query).bind(name, email, refer, message).run();
25+
26+
return new Response('Email submitted successfully', { status: 200 });
27+
};

0 commit comments

Comments
 (0)