Submit with JavaScript
Keep visitors on your page and show your own success and error messages.
Submit with fetch and send the header Accept: application/json. FormRhythm then replies with JSON instead of a redirect. The endpoint accepts requests from any website, so no proxy is needed.
With form data
<form id="contact" action="https://www.formrhythm.com/f/YOUR_FORM_ID" method="POST">
<!-- your fields -->
<button type="submit">Send</button>
<p id="form-status" role="status"></p>
</form>
<script>
const form = document.getElementById('contact');
const status = document.getElementById('form-status');
form.addEventListener('submit', async (event) => {
event.preventDefault();
status.textContent = 'Sending...';
const response = await fetch(form.action, {
method: 'POST',
body: new FormData(form),
headers: { Accept: 'application/json' },
});
if (response.ok) {
form.reset();
status.textContent = 'Thanks! Your message has been sent.';
} else {
const { error } = await response.json();
status.textContent = error || 'Something went wrong. Please try again.';
}
});
</script>With a JSON body
Handy when the data does not come from a form element:
await fetch('https://www.formrhythm.com/f/YOUR_FORM_ID', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify({
email: 'maya@example.com',
plan: 'pro',
source: 'pricing-page',
}),
});Responses
HTTP/1.1 201 Created
{
"message": "Form submitted successfully",
"redirectUrl": "https://www.formrhythm.com/f/YOUR_FORM_ID/success"
}redirectUrl is your custom redirect when one is set.
React and Next.js
The same approach works in any framework. This component works in React, and in Next.js as a client component.
'use client';
import { useState } from 'react';
const ENDPOINT = 'https://www.formrhythm.com/f/YOUR_FORM_ID';
export function ContactForm() {
const [status, setStatus] = useState<'idle' | 'sending' | 'sent' | 'error'>('idle');
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
setStatus('sending');
const response = await fetch(ENDPOINT, {
method: 'POST',
body: new FormData(event.currentTarget),
headers: { Accept: 'application/json' },
});
if (response.ok) {
event.currentTarget.reset();
setStatus('sent');
} else {
setStatus('error');
}
}
return (
<form onSubmit={handleSubmit}>
<input name="email" type="email" required />
<textarea name="message" required />
<button type="submit" disabled={status === 'sending'}>
{status === 'sending' ? 'Sending...' : 'Send'}
</button>
{status === 'sent' && <p>Thanks! We will be in touch.</p>}
{status === 'error' && <p>Something went wrong. Please try again.</p>}
</form>
);
}Is the endpoint a secret?
No. It only accepts new submissions, so it is safe to include in browser code.