{"id":365,"date":"2024-01-31T05:04:51","date_gmt":"2024-01-31T05:04:51","guid":{"rendered":"http:\/\/www.phpgang.com\/?p=365---4baa51b8-42cc-4eba-a760-b197d14d7e48"},"modified":"2024-01-31T05:04:51","modified_gmt":"2024-01-31T05:04:51","slug":"how-to-delete-comments-with-jquery","status":"publish","type":"post","link":"https:\/\/www.phpgang.com\/how-to-delete-comments-with-jquery_365.html","title":{"rendered":"How to delete comments with jQuery"},"content":{"rendered":"

Few days ago I wrote an article on how to comment with jQuery<\/a> and after that received some requests<\/a> from my readers to write something on comment deletion. Here I am going to write this article on how to remove comments with jQuery<\/a> without loading<\/a> complete page. I hope it will help you in your projects<\/a>.<\/p>\n

\"delete-comments-with-jQuery\"<\/p>\n\n\n\n
[wpdm_file id=41]<\/td>\n\n
\n
\n
\n
\u00a0\u00a0\u00a0Demo\u00a0\u00a0\u00a0\u00a0\u00a0<\/a>\u00a0\u00a0<\/span><\/div>\n<\/div>\n
<\/div>\n<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Database Details:<\/strong><\/p>\n

database name => phpgang
\ntable name => status,comments<\/p>\n

db.sql<\/strong><\/p>\n

Database file run in your mysql to create database and add data in table.<\/p>\n

CREATE TABLE `comments` (\r\n  `id` int(10) NOT NULL AUTO_INCREMENT,\r\n  `sid` int(10) NOT NULL,\r\n  `comment` text NOT NULL,\r\n  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,\r\n  PRIMARY KEY (`id`)\r\n) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;\r\n\r\nINSERT INTO `comments` VALUES (1, 1, 'WOW thats great..... :)', '2013-10-21 04:19:07');\r\n\r\nCREATE TABLE `status` (\r\n  `id` int(10) NOT NULL AUTO_INCREMENT,\r\n  `status` text NOT NULL,\r\n  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,\r\n  PRIMARY KEY (`id`)\r\n) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;\r\n\r\nINSERT INTO `status` VALUES (1, 'Vote in the Google India Impact Challenge for the 4 NGOs that you think are changing the world. Each of the 4 NGOs that win will receive a Global Impact Award worth approximately $500K USD, as well as support from Google to make their vision a reality. Visit g.co\/indiachallenge to vote now.', '2013-10-21 04:18:38');<\/pre>\n

db.php<\/strong><\/p>\n

Database configuration file edit database name, user and password as per your configuration.<\/p>\n

<?php\r\n    $connection = mysql_connect('localhost','phpgang','********') or die(mysql_error());\r\n    $database = mysql_select_db('phpgang') or die(mysql_error());\r\n?><\/pre>\n

index.php<\/strong><\/p>\n

include('db.php');\r\nif($_POST)\r\n{\r\n    $sid = mysql_real_escape_string($_POST['sid']);\r\n    $strSQL_Result  = mysql_query(\"delete from comments where id=$sid\");\r\n    exit;\r\n}\r\n$strSQL_Result  = mysql_query(\"select id,status from status LIMIT 1\");\r\n$row            = mysql_fetch_array($strSQL_Result);\r\n\r\n$sid         = $row['id'];\r\n$status     = $row['status'];\r\n$commentshow = \"\";\r\n$strSQL_Comment     = mysql_query(\"select id,comment from comments LIMIT 10\");\r\nwhile($rowcomm = mysql_fetch_array($strSQL_Comment))\r\n{\r\n    $id             = $rowcomm['id'];\r\n    $comment        = $rowcomm['comment'];\r\n    $commentshow    .= \"<div class='commentarea' id='div_$id'><div class='del'><a href='#' class='delete' id='$id'>x<\/a><\/div>\".$comment.\"<\/div>\";\r\n}\r\n<div class=\"status\">'.$status.'<\/div>\r\n<div id=\"commentbox\">\r\n'.$commentshow.'\r\n<\/div>';<\/pre>\n

jQuery<\/strong> required to delete comment:<\/p>\n

<script type=\"text\/javascript\" src=\"jquery-1.8.0.min.js\"><\/script>\r\n<script>\r\n$(document).ready(function() {\r\n$(\\'.delete\\').click(function(e)\r\n    {\r\n    var r = confirm(\"Want to delete?\");\r\n    if (r == true)\r\n    {\r\n        $(\"#div_\"+this.id).hide(\"slow\");\r\n        $.post(\"index.php\", {sid:this.id},function(data)\r\n        {\r\n\r\n        })\r\n    }\r\n    });            \r\n});\r\n<\/script><\/pre>\n

CSS<\/strong><\/p>\n

<style>\r\n.status\r\n{\r\n    width:350px;\r\n    font-size: 13px;\r\n    line-height: 18px;\r\n    font-family: \\'lucida grande\\',tahoma,verdana,arial,sans-serif;\r\n}\r\n.commentarea\r\n{\r\n    width:350px;\r\n    font-size: 13px;\r\n    line-height: 18px;\r\n    font-family: \\'lucida grande\\',tahoma,verdana,arial,sans-serif;\r\n    border: thin;\r\n    border-color: white;\r\n    border-style: solid;\r\n    background-color: hsl(0, 0%, 96%);\r\n    padding: 5px;\r\n}\r\n#comment\r\n{\r\n    width: 357px;\r\n    height: 23px;\r\n    font-size: 15px;\r\n}\r\n.del \r\n{\r\nfloat: right;\r\nmargin-right: 0px;\r\nmargin-top: -7px;\r\n}\r\n.del a {\r\ncolor: hsl(221, 44%, 41%);\r\ncursor: pointer;\r\ntext-decoration: none;\r\n}\r\n.del a:hover {\r\n    background: rgba(0,0,0,.5);\r\n    color: rgb(255,255,255);\r\n}\r\n<\/style><\/pre>\n
Facebook<\/a><\/blockquote><\/div><\/div>
Tutorial Categories:<\/strong>
\n \"jQuery\"<\/a><\/div>
\n \"PHP\"<\/a><\/div><\/div>","protected":false},"excerpt":{"rendered":"

Few days ago I wrote an article on how to comment with jQuery and after that received some requests from my readers to write something on comment deletion. Here I am going to write this article on how to remove comments with jQuery without loading complete page. I hope it will help you in your projects.
\n\"delete-comments-with-jQuery\"<\/p>\n","protected":false},"author":1,"featured_media":366,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"twitterCardType":"","cardImageID":0,"cardImage":"","cardTitle":"","cardDesc":"","cardImageAlt":"","cardPlayer":"","cardPlayerWidth":0,"cardPlayerHeight":0,"cardPlayerStream":"","cardPlayerCodec":"","footnotes":""},"categories":[38,3],"tags":[216,215,121,627],"_links":{"self":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts\/365"}],"collection":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":0,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/media\/366"}],"wp:attachment":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}