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
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
- onePlease, 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