-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.php
105 lines (89 loc) · 3.22 KB
/
post.php
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
<!DOCTYPE html>
<html lang="ru">
<head>
<?php
require_once "lib/mysql.php";
$sql = 'SELECT * FROM articles WHERE `id` = ?';
$query = $pdo->prepare($sql);
$query->execute([$_GET['id']]);
$article = $query->fetch(PDO::FETCH_OBJ);
$website_title = $article->title;
require "blocks/head.php"
?>
</head>
<body>
<?php require "blocks/header.php"?>
<main>
<?php
echo "<div class='post'>
<h1>" . $article->title . "</h1>
<p>" . $article->anons . "</p><br>
<p>" . $article->full_text . "</p>
<p class='avtor'>Автор: <span>" . $article->avtor . "</span></p><br>
<p><b>Время публикации:</b>" . date("H:i:s", $article->date) . "</p>
</div>";
?>
<h3>Комментарии</h3>
<form>
<label for="username">Ваше имя</label>
<?php if(isset($_COOKIE['login'])): ?>
<input type="text" name="username" id="username" value="<?=$_COOKIE['login']?>">
<?php else: ?>
<input type="text" name="username" id="username">
<?php endif; ?>
<label for="mess">Сообщение</label>
<textarea name="mess" id="mess"></textarea>
<div class="error-mess" id="error-block"></div>
<button type="button" id="mess_send">Добавить комментарий</button>
</form>
<div class="comments">
<?php
require_once "lib/mysql.php";
$sql = 'SELECT * FROM comments WHERE `article_id` = ? ORDER BY id DESC';
$query = $pdo->prepare($sql);
$query->execute([$_GET['id']]);
$comments = $query->fetchAll(PDO::FETCH_OBJ);
foreach($comments as $el) {
echo "<div class='comment'>
<h2>" . $el->name . "</h2>
<p>" . $el->mess . "</p>
</div>";
}
?>
</div>
</main>
<?php require "blocks/aside.php"?>
<?php require "blocks/footer.php"?>
<script>
$('#mess_send').click(function() {
let name = $('#username').val();
let mess = $('#mess').val();
$.ajax({
url: 'ajax/comment_add.php',
type: 'POST',
cache: false,
data: {
'username': name,
'mess': mess,
'id': '<?=$_GET['id']?>'
},
dataType: 'html',
success: function(data) {
if(data === "Done") {
$(".comments").prepend(`<div class='comment'>
<h2>${name}</h2>
<p>${mess}</p>
</div>`);
$("#mess_send").text("Все готово");
$("#error-block").hide();
$('#mess').val("");
} else {
$("#error-block").show();
$("#error-block").text(data);
}
}
});
});
</script>
</body>
</html>