-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathcontact.js
76 lines (68 loc) · 2.51 KB
/
contact.js
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
75
76
import React, { useState } from 'react';
import styles from '../styles/Contact.module.css'
const Contact = () => {
const [name, setname] = useState('')
const [email, setemail] = useState('')
const [phone, setphone] = useState('')
const [desc, setdesc] = useState('')
const handleSubmit = (e) => {
e.preventDefault()
const data = {phone, name, email, desc};
fetch('http://localhost:3000/api/postcontact', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.text())
.then(data => {
alert("Thanks for contacting us");
setphone('')
setname('')
setdesc('')
setemail('')
})
.catch((error) => {
console.error('Error:', error);
});
}
const handleChange = (e) => {
if (e.target.name == 'phone') {
setphone(e.target.value)
}
else if (e.target.name == 'email') {
setemail(e.target.value)
}
else if (e.target.name == 'desc') {
setdesc(e.target.value)
}
else if (e.target.name == 'name') {
setname(e.target.value)
}
}
return <div className={styles.container}>
<h1>Contact Us</h1>
<form onSubmit={handleSubmit}>
<div className={styles.mb3}>
<label htmlFor="name" className={styles.formlabel}>Enter your name</label>
<input className={styles.input} type="text" value={name} onChange={handleChange} id="name" name='name' aria-describedby="emailHelp" />
</div>
<div className={styles.mb3}>
<label htmlFor="email" className={styles.formlabel}>Email address</label>
<input className={styles.input} type="email" value={email} onChange={handleChange} name='email' id="email" aria-describedby="emailHelp" />
<div id="emailHelp" className={styles.formtext}>We will never share your email with anyone else.</div>
</div>
<div className={styles.mb3}>
<label htmlFor="phone" className={styles.formlabel}>Phone</label>
<input className={styles.input} type="phone" value={phone} onChange={handleChange} name='phone' id="phone" required />
</div>
<div className={styles.mb3}>
<label className={styles.formlabel} htmlFor="desc">Elaborate your concern</label>
<textarea className={styles.input} value={desc} onChange={handleChange} name='desc' id="desc" />
</div>
<button type="submit" className={styles.btn}>Submit</button>
</form>
</div>;
};
export default Contact;