-
Notifications
You must be signed in to change notification settings - Fork 1
/
react-form-with-recaptcha.jsx
74 lines (68 loc) · 1.68 KB
/
react-form-with-recaptcha.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, { useState } from "react";
import axios from "axios";
// Install dependency - npm install react-google-recaptcha
import ReCAPTCHA from "react-google-recaptcha";
export default function Forms() {
const recaptchaRef = React.createRef();
// Input Change Handling
const [inputs, setInputs] = useState({
email: "",
subject: "",
message: "",
});
const handleOnChange = (event) => {
event.persist();
setInputs((prev) => ({
...prev,
[event.target.id]: event.target.value,
}));
};
// Server State Handling
const handleOnSubmit = (event) => {
event.preventDefault();
axios({
method: "POST",
url: "https://formbold.com/s/FORM_ID",
data: {...inputs, 'g-recaptcha-response': recaptchaRef.current.getValue()},
})
.then((r) => {
console.log("hello");
})
.catch((r) => {
console.log("error");
});
};
return (
<form onSubmit={handleOnSubmit}>
<input
onChange={handleOnChange}
value={inputs.email}
id="email"
type="email"
name="email"
placeholder="Email"
/>
<input
onChange={handleOnChange}
value={inputs.subject}
id="subject"
type="text"
name="subject"
placeholder="Subject"
/>
<textarea
onChange={handleOnChange}
value={inputs.message}
id="message"
name="message"
placeholder="Type your message"
/>
<ReCAPTCHA
ref={recaptchaRef}
// Insert reCAPTCHA site key here
sitekey="your recaptcha site key"
/>
<button type="submit"> Send Message </button>
</form>
);
}