Skip to content

Commit a2c7572

Browse files
committed
Posting FormData via Fetch API
1 parent e511678 commit a2c7572

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

DOM/posting-formData.html

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Posting FormData via Fetch API</title>
8+
<style>
9+
#output input {
10+
margin: 5px;
11+
}
12+
.radio {
13+
display: flex;
14+
flex-direction: column;
15+
justify-items: center;
16+
}
17+
</style>
18+
</head>
19+
<body>
20+
<div id="output"></div>
21+
22+
<script>
23+
const output = document.getElementById("output");
24+
output.innerHTML = `
25+
<h1> Posting FormData via Fetch API
26+
</h1>
27+
</br>
28+
<form name='order'>
29+
<label>
30+
Name: <input name="fullname" type="text" value="rakib">
31+
</label>
32+
33+
</br>
34+
<label>
35+
Email: <input name="email" type="email" >
36+
</label>
37+
<label>
38+
</br>
39+
<label>
40+
ID:<input name="id" type="number" >
41+
</label>
42+
<label>
43+
44+
45+
</br>
46+
<label>
47+
Select Dept:
48+
<select name="dept">
49+
<option value="CSE">CSE</option>
50+
<option value="EEE">EEE</option>
51+
<option value="BBA">BBA</option>
52+
53+
</select>
54+
</label>
55+
</br>
56+
57+
<div class="radio">
58+
<label>
59+
<input
60+
type="radio"
61+
name="year"
62+
id=""
63+
value="first"
64+
checked="checked"
65+
/>
66+
first year</label
67+
>
68+
<label>
69+
<input
70+
type="radio"
71+
name="year"
72+
id=""
73+
value="second"
74+
75+
/>
76+
second year</label
77+
>
78+
<label>
79+
<input
80+
type="radio"
81+
name="year"
82+
id=""
83+
value="third"
84+
85+
/>
86+
third year</label
87+
>
88+
<label>
89+
<input
90+
type="radio"
91+
name="year"
92+
id=""
93+
value="fourth"
94+
95+
/>
96+
fourth year</label
97+
>
98+
</div>
99+
<label>
100+
<input name="submit" type="submit">
101+
</label>
102+
103+
104+
</form>
105+
`;
106+
107+
const form = document.forms.order;
108+
109+
function handleSubmit(event) {
110+
event.preventDefault();
111+
console.log("killer");
112+
113+
const formData = new FormData(event.target);
114+
115+
//query string
116+
117+
const asString = new URLSearchParams(formData).toString();
118+
console.log(asString);
119+
120+
//JSON
121+
const asJSON = JSON.stringify(Object.fromEntries(formData));
122+
console.log(asJSON);
123+
124+
const postData = fetch("https://jsonplaceholder.typicode.com/posts", {
125+
method: "POST",
126+
// headers: { "Content-Type": "application/x-www-form-urlencoded" },
127+
// body: asString,
128+
129+
headers: { "Content-Type": "application/json" },
130+
body: asJSON,
131+
})
132+
.then((response) => response.json())
133+
.then((data) => console.log(data));
134+
}
135+
136+
form.addEventListener("submit", handleSubmit);
137+
</script>
138+
</body>
139+
</html>

0 commit comments

Comments
 (0)