-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpost.php
More file actions
184 lines (158 loc) · 6.45 KB
/
Copy pathpost.php
File metadata and controls
184 lines (158 loc) · 6.45 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
require "header.php";
$tag = '';
if (isset($_GET['tag'])) {
$tag = $_GET['tag'];
}
?>
<?php
if (isset($_GET['id'])) {
?>
<link rel="stylesheet" href="css/post.min.css?v.1">
<?php } ?>
<main class="post-page">
<style>
.hide {
display: none;
}
.post-div{
width: 100%;
}
</style>
<?php
if (!isset($_GET['id'])) {
?>
<div class="card col-lg-9 mx-auto co">
<div class="card-body">
<h5 class="card-title">Create Post</h5>
<div id="toast"></div>
<form id="postForm" enctype="multipart/form-data">
<div class="mb-3">
<label for="communitySelect">Post to</label>
<select class="form-select" id="communitySelect" name="community">
<option value="account" <?= isset($_GET['story']) ?: "selected" ?>>Your page</option> <!-- added a default option -->
<option value="story" <?= !isset($_GET['story']) ?: "selected" ?>>Your story</option>
</select>
</div>
<div class="mb-3">
<a href="" data-lightbox="index">
<img id="previewImage" class="hide img-thumbnail" src="#" alt="Preview Image" loading="lazy" style="max-height: 152px; max-width: 200px;">
</a>
<i class="fa fa-trash hide"></i>
</div>
<div class="row my-2 col-sm-6">
<div class="col-4 my-2">
<label for="image">
<i class="fa fa-image fa-2x"></i>
</label>
<p>image</p>
</div>
</div>
<div class="mb-3">
<label for="postText">Post Text</label>
<textarea class="form-control" id="postText" name="postText" placeholder="Enter some text for your post"></textarea> <!-- added a placeholder -->
</div>
<details class="mb-1" <?= $tag ? 'open' : '' ?>>
<summary>Add Tags</summary>
<div class="mb-3">
<label for="tagsInput">Tags</label>
<input type="text" class="form-control" id="tagsInput" name="tags" value="<?= $tag ?>" placeholder="tag1,tag2,tag3">
</div>
</details>
<div class="mb-3" hidden>
<input type="file" class="form-control" id="image" name="image" accept="image/*">
</div>
<input type="submit" id="upload" class="btn bg" name="upload" value="Submit" />
</form>
</div>
</div>
<?php } ?>
<div id="main-post">
<noscript style="color:red">this site requires javascript to function</noscript>
</div>
</main>
<script src="./lib/lightbox/js/lightbox.min.js" defer></script>
<?php
require 'footer.php';
?>
<script>
$(document).ready(function() {
<?php
if (isset($_GET['id'])) {
?>
let post_url = './inc/post.inc.php?id=' + '<?= $_GET['id'] ?>&';
mainload(post_url)
<?php } ?>
// Preview image on file selection
$("#image").change(function() {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$("#previewImage").attr("src", e.target.result).removeClass("hide");
$("[data-lightbox]").attr("href", e.target.result);
$(".fa-trash").removeClass("hide");
};
reader.readAsDataURL(this.files[0]);
}
});
// Handle form submission
$("#postForm").submit(function(e) {
e.preventDefault();
// Get form data
var formData = new FormData(this);
formData.append("type", $("#image").get(0).files.length > 0 ? "img" : "txt");
formData.append("upload", 'post');
// Submit form using AJAX
$.ajax({
url: "./inc/post.inc.php",
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function(response) {
// Handle success response
if (response.type == 'success') {
// Show success toast
showToast("Success!", "Your post has been created.", "success");
// Clear fields and image
$("#previewImage").attr("src", '').addClass('hide');
$(".fa-trash").addClass("hide");
$('#postText').val('');
$('#tagsInput').val('');
$('#image').val('');
} else {
// Show error toast
showToast("Error!", `error: ${response.message || response.msg}`, "warning");
}
},
error: function(xhr, status, error) {
// Handle error
console.error(error);
// Show error toast
showToast("Error!", "An error occurred while creating your post.", "warning");
}
});
});
// Function to display a toast notification
function showToast(title, message, type) {
let toast = `<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body mb-3 p-2 rounded text-white bg-${type}">
${message}
</div>
</div>`;
$("#toast").html(toast);
$("#toast").fadeOut(5000, function() {
$(this).empty().show();
});
}
function clearImage() {
$("#previewImage").attr("src", '').addClass('hide');
$("[data-lightbox]").attr("href", '');
$(".fa-trash").addClass("hide");
$('#image').val('');
}
$('.fa-trash').click(function() {
clearImage();
});
});
</script>