foreach($comments as $comment){
echo
` <div class="comments__item">
<div class="comments__people">
<div class="comments__name"><?= $comment['name']?></div>
<div class="comments__surname"><?= $comment['surname']?></div>
</div>
<?php
if($comment['user_id'] == $_SESSION['user_id']){
?>
<div class="comments__control">
<a href="php/change_comment.php" class="comments__change">Изменить</a>
<a href="php/delete_comment.php" class="comments__delete">Удалить</a>
</div>
<?php
}
?>
<div class="comments__message"><?= $comment['message']?></div>
</div>`
}
const button = $('.create-comments__button');
function RunAjax(e){
e.preventDefault();
const xhr = new XMLHttpRequest();
const name = $('.create-comments__input--name').val();
const surname = $('.create-comments__input--surname').val();
const message = $('.create-comments__input--message').val();
$.ajax({
url: '../../php/comments.php',
type: 'POST',
data: {
name: name,
surname: surname,
message: message
},
success: function(result){
$(".comments__body").html(result);
}
});
}
button.click(RunAjax);
<?php
require 'config.php';
$name = $_POST['name'];
$surname = $_POST['surname'];
$message = $_POST['message'];
$query = $conn->prepare("INSERT INTO `comments` (`name`, `surname`, `message`) VALUES(:name, :surname, :message)");
$query->execute(['name' => $name, 'surname' => $surname, 'message' => $message]);
$comments = [];
$query = $conn->query("SELECT * FROM `comments`");
$comments = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($comments as $comment){
?>
<div class="comments__item">
<div class="comments__people">
<div class="comments__name"><?= $comment['name']?></div>
<div class="comments__surname"><?= $comment['surname']?></div>
</div>
<?php
if($comment['user_id'] == $_COOKIE['user_id']){
?>
<div class="comments__control">
<a href="" class="comments__change">Изменить</a>
<a href="" class="comments__delete">Удалить</a>
</div>
<?php
}
?>
<div class="comments__message"><?= $comment['message']?></div>
</div>
<?php
}
?>
const button = document.querySelector('.create-comments__button');
const xhr = new XMLHttpRequest();
button.onclick = (e) =>{
e.preventDefault();
const name = document.querySelector('.create-comments__input--name').value;
const surname = document.querySelector('.create-comments__input--surname').value;
const message = document.querySelector('.create-comments__input--message').value;
xhr.open('post', '../../php/comments.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send("name=" + name + "&surname=" + surname + "&message=" + message);
}
<?php
$comments = [];
$query = $conn->query("SELECT * FROM `comments`");
$comments = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($comments as $comment){
?>
<div class="comments__item">
<div class="comments__people">
<div class="comments__name"><?= $comment['name']?></div>
<div class="comments__surname"><?= $comment['surname']?></div>
</div>
<div class="comments__message"><?= $comment['message']?></div>
</div>
<?php
}
?>
<a href="php/delete_comment.php?id=<?=$comment['id']?>" class="comments__delete">Удалить</a>
$user_id = $_COOKIE['user_id'];
$delete = $conn->prepare("DELETE FROM `comments` WHERE `user_id` = :user_id AND `id` = :id ");
$delete->execute(['user_id' => $user_id, 'id' => $_GET['id']]);
header('Location: ../index.php');
Когда я два раза нажму на .change-comments__button, то у меня создается такой же комментарий на сайте, ну не в бд. Я подозреваю что некорректно работает это область кода: