Written by: Geoffrey Callaghan
Create a custom contact form using Framer
Create a custom contact form using Framer
Creating a custom contact form using Framer involves designing the form in the Framer interface and then adding the necessary code to handle form submissions. Here’s a step-by-step guide:
Create a new project:
Design the form:
Add Input Fields:
type
(text, email, etc.), placeholder
, and required
.Add a Submit Button:
To handle form submissions, you’ll need to add some custom code. Here’s how to do it in Framer:
Select the form or button:
Add a code component:
Edit the Code Component:
import { useState } from "react";
export function ContactFormHandler() {
const [formData, setFormData] = useState({
name: "",
email: "",
message: ""
});
const [responseMessage, setResponseMessage] = useState("");
const handleInputChange = (e) => {
const { name, value } = e.target;
setFormData({ ...formData, [name]: value });
};
const handleSubmit = async (e) => {
e.preventDefault();
try {
const response = await fetch("/api/contact", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(formData)
});
if (response.ok) {
setResponseMessage("Form successfully submitted");
setFormData({
name: "",
email: "",
message: ""
});
} else {
setResponseMessage("Error submitting form");
}
} catch (error) {
setResponseMessage("Error submitting form");
}
};
return (
<form onSubmit={handleSubmit}>
<label>
Name:
<input
type="text"
name="name"
value={formData.name}
onChange={handleInputChange}
required
/>
</label>
<label>
Email:
<input
type="email"
name="email"
value={formData.email}
onChange={handleInputChange}
required
/>
</label>
<label>
Message:
<textarea
name="message"
value={formData.message}
onChange={handleInputChange}
required
></textarea>
</label>
<button type="submit">Send</button>
<p>{responseMessage}</p>
</form>
);
}
You’ll need a backend service to handle the form submissions. You can use services like Render, Netlify Functions, Vercel, or your own server.
Create an API Endpoint:
Deploy the Server:
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.json());
app.post("/api/contact", (req, res) => {
const { name, email, message } = req.body;
// Process the form data (e.g., send an email or save to a database)
console.log("Form data received:", { name, email, message });
res.status(200).json({ message: "Form submission received" });
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Preview and Test:
Deploy the Framer Project:
By following these steps, you can create and deploy a custom contact form using Framer, complete with backend handling for form submissions.