-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateBlog.html
88 lines (81 loc) · 3.01 KB
/
updateBlog.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Blog Login</title>
<link rel="stylesheet" href="backend/public/assets/css/addblog.css" />
<script type="module" src="backend/public/assets/js/pages/blog.js"></script>
<style>
textarea {
resize: none;
}
</style>
</head>
<body>
<div class="login-container">
<div class="login-form">
<h2>Add Your Blog</h2>
<form id="uploadForm" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Title:</label>
<input type="text" id="title" name="title" required />
<small class="error-field" id="error-title"></small>
</div>
<div class="form-group">
<label for="image">Image:</label>
<input type="file" id="image" name="image" accept="image/*" required />
<small class="error-field" id="error-image"></small>
<img id="preview" class="image-preview" alt="Preview" />
</div>
<div class="form-group">
<label for="content">Content:</label>
<textarea id="content" name="content" rows="10" required></textarea>
<small class="error-field" id="error-content"></small>
</div>
<button type="submit" class="login-btn">Post</button>
</form>
<div id="uploadProgress" class="upload-progress"></div>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", async () => {
const params = new URLSearchParams(window.location.search);
const pid = params.get("pid");
async function fetchBlogData(pid) {
try {
const response = await fetch(
`http://localhost/blogit/backend/routes/blogRoute.php?pid=${encodeURIComponent(pid)}`,
);
if (!response.ok) {
throw new Error(`Error: ${response.statusText}`);
}
const blogData = await response.json();
console.log(blogData);
// Populate the form fields with the fetched data
document.getElementById("title").value = blogData.blog.title;
document.getElementById("content").value = blogData.blog.content;
} catch (error) {
console.error("Failed to fetch blog data:", error);
}
}
// If a pid is present, fetch the blog data
if (pid) {
await fetchBlogData(pid);
}
});
document.getElementById("image").addEventListener("change", function (e) {
const file = e.target.files[0];
const preview = document.getElementById("preview");
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
preview.src = e.target.result;
preview.style.display = "block";
};
reader.readAsDataURL(file);
}
});
</script>
</body>
</html>