-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.html
67 lines (61 loc) · 2.12 KB
/
form.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Homework</title>
<link rel="stylesheet" href="formStyles.css">
</head>
<body>
<h1 class="title">Add New Homework</h1>
<form id="homework-form">
<label>
Image URL:
<input type="text" id="img-url" placeholder="Enter image URL" required>
</label>
<br><br>
<label>
Title:
<input type="text" id="homework-title" placeholder="Enter title" required>
</label>
<br><br>
<label>
Description:
<input type="text" id="homework-description" placeholder="Enter description" required>
</label>
<br><br>
<button type="button" onclick="submitHomework()">Submit</button>
</form>
<script>
const apiUrl = "https://673f23ffa9bc276ec4b75913.mockapi.io/site/v1/Sites"
async function submitHomework() {
const imgName = document.getElementById("img-url").value
const img = `/img/${imgName}.jpg`
const title = document.getElementById("homework-title").value
const description = document.getElementById("homework-description").value
const newHomework = {
img: img,
title: title,
description: description
}
try {
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(newHomework)
})
if (response.ok) {
window.location.href = "index.html"
} else {
alert("Failed to submit homework. Please try again.")
}
} catch (error) {
console.error("Error submitting homework:", error)
alert("An error occurred. Please try again later.")
}
}
</script>
</body>
</html>