Skip to content

Commit

Permalink
update leadform
Browse files Browse the repository at this point in the history
  • Loading branch information
theWhiteWulfy committed Aug 8, 2024
1 parent 757f4aa commit 74916e9
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/pages/api/leadform.ts
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 });
};

0 comments on commit 74916e9

Please sign in to comment.