Hello. There is such a problem, you need to organize the removal of comments using AJAX (without reloading the page). Maybe someone knows how to write a script? I myself am very weak in JS. I would be very grateful to all who answered.

    2 answers 2

    For example on the server we generate html of the form:

    <?foreach($comments as $comment):?> <div class="comment"> <div class="author"> <?=$comment['author']?> </div> <div class="content"> <?=$comment['content']?> </div> <!-- Ссли ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒ ΠΈΠΌΠ΅Π΅Ρ‚Π΅ ΠΏΡ€Π°Π²ΠΎ ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ этот ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ --> <?if($_SESSION['username'] === $comment['author']):?> <!-- ΠΏΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ ΠΊΠ½ΠΎΠΏΠΊΡƒ удалСния --> <button class="remove" id="remove_<?=$comment['id']?>"> <?endif?> </div> <?endforeach?> 

    Accordingly, now having selected all the buttons on the css class, we can get the id of each of them for transmission to the server, but now for the client code.

     $(".comment .remove").on('click', function(){ var $button = $(this); var commentId = this.id.slice(7); // ΠΎΠ±Ρ€Π΅ΠΆΠ΅ΠΌ id Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ число // Π΄Π΅Π»Π°Π΅ΠΌ POST запрос Π½Π° сСрвСр $.post("/comments_remove.php", {id : commentId}, function( serverResponse ){ if( serverResponse === 'ok' ) { // Ссли сСрвСр ΠΎΡ‚Π²Π΅Ρ‚ΠΈΠ» ok - всС Ρ…ΠΎΡ€ΠΎΡˆΠΎ $button.parent().remove(); // удаляСм HTML коммСнтария // Ρ‚ΡƒΡ‚ ΠΌΠΎΠΆΠ΅ΠΌ Π΅Ρ‰Π΅ Ρ‡Ρ‚ΠΎ-Π»ΠΈΠ±ΠΎ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ } else { // Π² ΠΏΡ€ΠΎΡ‚ΠΈΠ²Π½ΠΎΠΌ случаС ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ Π½Π΅ Π±Ρ‹Π» ΡƒΠ΄Π°Π»Π΅Π½ // Ρ‚ΡƒΡ‚ ΠΌΠΎΠΆΠ΅ΠΌ ΠΊΠ°ΠΊ-Ρ‚ΠΎ ΡΠΎΠΎΠ±Ρ‰ΠΈΡ‚ΡŒ ΠΎΠ± ошибкС } }) }) 

    Well, on the server, comment_remove.php, something like this:

     // mysql connection here.... if($_SERVER[ 'REQUEST_METHOD' ] === 'POST') { if( is_numeric($_POST['id']) ) { $commentId = (int)$_POST['id']; $commentDbData = mysql_query('SELECT * FROM comments WHERE id=' . $commentId); $comment = mysql_fetch_assoc($commentDbData); if(!empty($comment)) { // Ссли Ρ‚Π°ΠΊΠΎΠΉ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ Π΄Π΅ΠΉΡΡ‚Π²ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ сущСствуСт // ΠΏΠ΅Ρ€Π΅ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΠΌ - ΠΈΠΌΠ΅Π΅Ρ‚ Π»ΠΈ ΠΏΡ€Π°Π²ΠΎ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒ ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ этот ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ if($comment[ 'author' ] === $_SESSION[ 'username' ]) { if( mysql_query("DELETE FROM comments WHERE id=" . $commentId) ) { echo 'ok'; // всС Π² порядкС, ΠΎΠΏΠΎΠ²Π΅Ρ‰Π°Π΅ΠΌ ΠΊΠ»ΠΈΠ΅Π½Ρ‚Π° } } } } } 

    Well, something like this, if the logic of deletion error handling is not planned, then the server can be made much simpler, something like:

      mysql_query('DELETE FROM comments WHERE id=' . (int)$_POST['id'] . ' AND author="' . $_SESSION[ 'author' ] . '"'); echo 'ok'; 

    In any case, you need to be able to get the ID of a specific comment and do not forget to double-check everything that the client on the server "told" us.

    Good luck with the implementation.

    • Wow, how complicated is that. But thanks for the detailed explanation, I'll try to figure it out. - Nikolay Nikonov
    • one
      Please, in fact, everything is very simple, it only seems complicated. - Zowie

    Use jQuery, many times easier, if you understand programming, you can easily figure it out in a few hours (a lot of documentation is in Russian). The general algorithm is approximately as follows: You find an HTML element containing a comment (which you will need to delete later), delete, send an AJAX request to the server that will contain the comment ID, the server accepts, deletes from the database, returns the answer to the client that everything is "ok" . Everything.

    • Yes, I know the algorithm, the problem is that there are a lot of comments on the page, and how to write a unique id to each comment so that the script will understand which comment to hide from the page? - Nikolay Nikonov
    • Identify each comment in the database when inserting, and insert the identifier in the ID (html) when pulling out. - atata