When a form submits data with POST, the request message carries the encoded form data to the server. A useful interview answer traces that request beyond the browser: the server validates and processes the data, sends a response, and the browser displays success or errors. section 2.4
To explain where the request fits, distinguish these steps:
- Validate input on the client, then collect and encode the form fields according to the method and content type. section 2.4
- Send the request; validate again on the server before processing the data. section 2.4
Listing 2.8 Simple form implementation
const Form: React.FC = () => {
const [state, formAction, isPending] = useActionState<FormState, FormData>( #A
async (prevState, formData: FormData) => { #B
try {
const response = await fetch('https://api.web3forms.com/submit', { #C
method: 'POST',
body: formData as BodyInit, #D
});
const data = await response.json(); #E
if (!data.success) { #F
throw new Error(data.message);
}
return { #G
data,
error: null,
};
} catch (error) { #H
return {
data: null,
error: `There was a problem with your submission: ${error}`,
};
}
},
{ data: null, error: null } #I
);
return (
<form action={formAction}> #J
<input #K
type="hidden"
name="access_key"
value={import.meta.env.VITE_ACCESS_KEY} #L
/>
<label htmlFor="name">Name:</label>
<input #M
type="text"
name="name"
required
pattern="[A-Za-z\s]+" #N
title="Only letters and spaces allowed."
/>
<label htmlFor="email">Email:</label>
<input type="email" name="email" required /> #O
<input type="submit" value="Submit" disabled={isPending} /> #P
{state.error && <div>{state.error}</div>} #Q
{state.data && ( #R
<div>Form submitted successfully: ${JSON.stringify(state.data)}</div>
)}
</form>
);
}
The React example sends FormData with fetch using method: 'POST', then handles either the returned success data or a submission error. section 2.4

















