-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
757f4aa
commit 74916e9
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export const prerender = false; //This will not work without this line | ||
|
||
import type { APIRoute, APIContext } from 'astro'; | ||
|
||
export const POST: APIRoute = async ({ request, locals}: APIContext) => { | ||
const data = await request.formData(); | ||
const name = data.get("usrname"); | ||
const email = data.get("email"); | ||
const message = data.get("msg"); | ||
const refer = data.get("ref"); | ||
|
||
if (!locals || !locals.runtime || !locals.runtime.env || !locals.runtime.env.DB) { | ||
return new Response('Database not configured', { status: 500 }); | ||
} | ||
|
||
const { DB } = locals.runtime.env; | ||
|
||
if (!name || !email || !message || !refer) { | ||
return new Response('Missing required fields', { status: 400 }); | ||
} | ||
|
||
// Call a function to save the email to the D1 database | ||
const query = 'INSERT INTO leads (name, email, refer, message, timestamp) VALUES (?1, ?2, ?3, ?4, CURRENT_TIMESTAMP)'; | ||
await DB.prepare(query).bind(name, email, refer, message).run(); | ||
|
||
return new Response('Email submitted successfully', { status: 200 }); | ||
}; |