The site implemented ajax adding and deleting comments in the database, as well as removing the comment from the page. It was all simple. The problem is the output of the comment just written without rebooting. There is a block code with comments:
<? foreach ($comments as $comment) : ?> <div class="comments"> <div class="row comtext"> <div class="comname"> <?=$comment['name'];?> <br> <?=formatTime($comment['date']);?> </div> <div class="comtent"> <?=$comment['text'];?> </div> <div class="delete"> <button class="btn btn-sm btn-outline-light" id="<?=$comment['id']?>">Удалить</button> </div> </div> </div> <? endforeach;?> And a query to the database
$sqlComm = 'SELECT comments.id, comments.author_id, comments.date, comments.text, comments.post_id, users.name FROM comments JOIN posts ON posts.id = comments.post_id JOIN users ON users.id = comments.author_id WHERE posts.id = ' . $_GET['id'] . ' ORDER BY comments.date DESC'; $ comments = $ db-> query ($ sqlComm);
Also here is the code to add a comment. I suspect that the block with the output of this comment can be shoved into .done ():
$(document).ready(function () { $('button#add').on('click', function () { var text = $('textarea.text').val(); var post_id = $('input.post_id').val(); var user = $('input.user').val(); $.ajax({ method: "POST", url: "../newcom.php", data: {user: user,text: text, post_id: post_id} }) .done(function () { $('textarea.text').val(''); }) }) }); So, tell me how to properly pull the record from the database and display it in the comment block? Thank.