After trying not to redirect the user that has submitted the form, I had trouble sending the form to my form service.
I setup a Formspark form to use on framer.com. Framer.com forces me to use Typescript which I don't know (I only know HTML CSS and JS right now). I gathered ideas and random information from Formspark's documentation and sitepoint's community. I would really (really) appreciate it if someone created some free time to help solve my problem.
Here's my form (simplified):
import React, { useEffect, useRef, useState } from "react"
import styled from "styled-components"
const SalesForm = styled.form`
/*styles*/
`
const FormInput = styled.input`
/*styles*/
`
const Button = styled.button`
/*styles*/
`
const SuccessMessage = styled.div`
/*styles*/
`
const ErrorMessage = styled.div`
/*styles*/
`
export default function Form(props) {
const captchaRef = useRef<HTMLDivElement>(null)
const [token, setToken] = useState<string | null>(null)
const [message, setMessage] = useState<string | null>(null)
const [isSuccess, setIsSuccess] = useState<boolean | null>(null)
useEffect(() => {
console.log("Initializing hCaptcha...")
if (window.hcaptcha) {
window.hcaptcha.render(captchaRef.current!, {
sitekey: "mycaptchacode",
callback: (token: string) => {
console.log("hCaptcha token generated:", token)
setToken(token)
},
})
}
}, [])
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
if (!token) {
alert("Please verify that you are human.")
return
}
const formData = new FormData(event.currentTarget)
console.log("Form Data: ", Array.from(formData.entries()))
try {
const response = await fetch("https://submit-form.com/formid", {
method: "POST",
body: formData,
})
if (response.ok) {
setMessage("Form successfully submitted!")
setIsSuccess(true)
event.currentTarget.reset()
} else {
const errorText = await response.text()
console.error("Error response: ", errorText)
setMessage(
"There was a problem with your submission: " + errorText
)
setIsSuccess(false)
}
} catch (error) {
console.error("Submission error: ", error)
setMessage(
"There was an error submitting the form: " + error.message
)
setIsSuccess(false)
}
}
return (
<SalesForm id="sales-form" onSubmit={handleSubmit}>
<script src="https://js.hcaptcha.com/1/api.js" async defer></script>
<FormInput type="text" id="comp-name" name="comp-name" placeholder="comp-name" required/>
<div ref={captchaRef}></div>
<Button type="submit">Send</Button>
{message &&
(isSuccess ? (
<SuccessMessage>{message}</SuccessMessage>
) : (
<ErrorMessage>{message}</ErrorMessage>
))}
</SalesForm>
)
}
I'd like to give a bit more information:
- handleSubmit was working when it didnt have anything else than the 1st "if" statement that forces the user to verify captcha (therefore, my form had method and action attributes), it didnt also have "async". I added those other lines to stay on the same page after submitting. I was just using a
<input type="hidden" name="_redirect" value="redirecturl.com" />
. Not staying on the same page was the main thing that dragged me all the way here. I don't want the user to be redirected. - Based on all of the above, I suspect that using captcha isn't the problem. And my form service was working until I tried not to redirect.
- "Failed to fetch" is the error that displays below the form after I tried to send it.
I would really appreciate your help.