php ajax delete parent comment with comment reply
I can Manage comment display with reply. But I have issues about deleting parent comment with reply. You can understand from below code.
This is comment.php file.
<?php
require_once '../vendor/autoload.php';
include "../lib/Database.php";
include "../config/config.php";
include "../helpers/Format.php";
$db = new Database();
$fm = new Format();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = mysqli_real_escape_string($db->link,
$fm->validation($_POST['sender']));
$postComment = mysqli_real_escape_string($db->link,
$fm->validation($_POST['comment']));
$commentId =mysqli_real_escape_string($db->link,
$fm->validation($_POST['commentId']));
if (!empty($name) && !empty($postComment)) {
$commentQuery = "INSERT INTO `comment`(parent_id,comment,sender)
VALUES('$commentId','$postComment','$name')";
$commentsResult = $db->insert($commentQuery);
if ($commentsResult) {
$msg = "<p class='alert alert-success'><span class='fa-layers
fa-fw'><i class='fas fa-check-circle fa-2x'></i></span>success</p>";
$status = array(
'error' => 0,
'message' => $msg
);
} else {
$msg = "<p class='alert alert-danger'><i class='fas fa-times-
circle fa-2x'></i>deny</p>";
$status = array(
'error' => 1,
'message' => $msg
);
}
} else {
$msg = "<p class='alert alert-warning'><i class='fas fa-bell fa-2x'>
</i>empty field</p>";
$status = array(
'error' => 2,
'message' => $msg
);
}
echo json_encode($status);
}
And display comments with reply is like that
<?php
ini_set('memory_limit', '1024M');
require_once __DIR__.'/../vendor/autoload.php';
include __DIR__.'/../lib/Database.php';
include __DIR__.'/../config/config.php';
include __DIR__.'/../lib/Session.php';
include __DIR__.'/../helpers/Format.php';
$db = new Database();
$fm = new Format();
$commentQuery = "SELECT * FROM `comment` WHERE `parent_id`='0' ORDER BY `id`
DESC";
$result = $db->select($commentQuery);
$commentReply = '';
if ($result) {
while ($cmResult = $result->fetch_assoc()) {
$commentReply .= ' <div class="panel panel-default">
<div class="panel-heading">
<div class="fa-2x text-right" style="border: none;">
<button class=" btn btn-default delClick" id="
'.$cmResult['id'].'">
<span class="fa-layers fa-fw"
style="background:Mistyrose; border: none;">
<i class="fa-inverse fas fa-times-circle"
style="color: black;" data-fa-transform="shrink-3"> </i>
</span>
</button>
</div>
<h4>
<span class= "fa-layers fa-fw">
<i class="fa-inverse fas fa-user-graduate fa-3x" style="
background:MistyRose" >
</i> </span> ' . $cmResult["sender"] . '
</h4>
<p class="date">' . $fm->DateFormat($cmResult["date"]) .
'</p>
</div>
<div class="panel-body">
<p class="comment">' . $fm-
>textShorten($cmResult["comment"], 70) . '</p>
<button type="button" class="btn btn-warning reply"
id="' . $cmResult["id"] . '">REPLY</button>
</div>
</div>';
$commentReply .= getReply($db, $fm, $cmResult['id']);
}
echo $commentReply;
}
function getReply($db, $fm, $parentId = 0, $margin = 0) {
//$fm = new Format();
//$db = new Database();
$commentReply = '';
$getQuery = "SELECT * FROM `comment` WHERE `parent_id`='$parentId'";
$result = $db->select($getQuery);
// $count = mysqli_num_rows($result);
if ($parentId == 0) {
$margin = 0;
} else {
$margin = $margin + 28;
}
if (!empty($result) || $result > 0) {
while ($cmResult = $result->fetch_assoc()) {
// $fm =new Format();
$cmDate = $fm->DateFormat($cmResult['date']);$shorten = $fm-
>textShorten($cmResult['comment'], 70);
$commentReply .= ' <div class="panel_wrapper">
<div class="panel panel-default" style="margin-left: '. $margin. 'px">
<div class="panel-heading">
<div class="fa-2x text-right" style="border: none;">
<button class=" btn btn-default delClick" id=" '.$cmResult['id'].'">
<span class="fa-layers fa-fw" style="background:Mistyrose; border: none;">
<i class="fa-inverse fas fa-times-circle" style="color: black;" data-fa-
transform="shrink-3">
</i>
</span>
</button>
</div>
<h4>
<span class="fa-layers fa-fw">
<i class="fa-inverse fas fa-user-graduate fa-3x"
data-fa-transform="shrink-8"
style="background:MistyRose">
</i>
</span>
'. $cmResult["sender"].'
</h4>
<p class="date">'.$cmDate.'</p>
</div>
<div class="panel-body">
<p class="comment">'. $shorten.'</p>
<button type="button" class="btn btn-warning reply" id="'.
$cmResult['id'].'">
REPLY
</button>
</div>
</div>
</div>';
$commentReply .= getReply($db, $fm, $cmResult['id'], $margin);
}
}
return $commentReply;
}
I submit the comment form using ajax request.It is working fine with success message.
comment.js
$(document).ready(function(){
//$('#all-comments').hide();
showComments();
$('#commentForm').on('submit', function(e){
e.preventDefault();
//showComments().disable();
var formData = $(this).serialize();
// var message = $('#Messages').val();
$.ajax({
url: "../scripts/comment.php",
method: "POST",
data:
formData,
dataType: "JSON",
success:function(response) {
if(!response.error) {
$('#commentForm')[0].reset();
$('#commentId').val('0');
showComments();
$('#message').show().fadeIn('500').html(response.message);
$('#message').show().fadeOut('500').html(response.message);
$('#message').show().fadeIn('500').html(response.message);
$('#message').show().fadeOut('500').html(response.message);
}else if (response.error) {
$('#message').show().fadeIn("500").html(response.message);
$('#message').show().fadeOut("500").html(response.message);
$('#message').show().fadeIn("500").html(response.message);
$('#message').show().fadeOut("500").html(response.message);
}
}
});
//return false;
});
$(document).on('click','.reply', function(){
var commentId = $(this).attr('id');
$('#commentId').val(commentId);
$('#name').focus();
});
});
function showComments() {
$.ajax({
url:"../scripts/disqus.php",
type:"POST",
//dataType: "JSON",
success:function(response) {
// $('#all-comments').show();
$('#showComments').html(response);
}
})
}
I would like to edit delete comment both parent and reply using ajax.I tried many times but can't make it work.If anybody help me to achieve this it will be great. thanks. what i tried for deleting comment as like below
delete.php
<?php
//include __DIR__.'/../lib/Session.php';
include __DIR__.'/../vendor/autoload.php';
include __DIR__.'/../config/config.php';
include __DIR__.'/../lib/Database.php';
$db = new Database();
$delQuery = "DELETE FROM comment WHERE parent_id= 0";
$dResult = $db->delete($delQuery);
/*echo "<pre>";
var_dump($dResult);
echo "</pre>";
die();*/
if ($dResult) {
$msg = "<p class='alert alert-success'>
<i class='fas fa-times-circle fa-2x'>
</i>Comment deleted Successfully!!
</p>";
$status = array(
'error' => 3,
'message' => $msg
);
} else {
$msg = "<p class='alert alert-info'>
<i class='fas fa-warning-circle fa-2x'>
</i>Comment cannot delete.
</p>";
$status = array(
'error' => 4,
'message' => $msg
);
}
echo json_encode($status);
the ajax request for delete query is like that but it didn't work
$(document).on('click','.delClick',function (e) {
e.preventDefault();
// var delId = $(this).attr('id');
deleteComment();
window.location='../Template/Excellent.php';
});
function deleteComment()
{
// var data = $(this).attr('id');
$.ajax({
url:"../scripts/disqus_template.php",
type: "POST",
// data: data,
dataType: "JSON",
success: function (response) {
if(!response.error) {
$('#delmessage').html(response);
$('#delmessage').fadeIn('slow').html(response.message);
}
}
});
return false;
}
php ajax comments sql-delete delete-row
add a comment |
I can Manage comment display with reply. But I have issues about deleting parent comment with reply. You can understand from below code.
This is comment.php file.
<?php
require_once '../vendor/autoload.php';
include "../lib/Database.php";
include "../config/config.php";
include "../helpers/Format.php";
$db = new Database();
$fm = new Format();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = mysqli_real_escape_string($db->link,
$fm->validation($_POST['sender']));
$postComment = mysqli_real_escape_string($db->link,
$fm->validation($_POST['comment']));
$commentId =mysqli_real_escape_string($db->link,
$fm->validation($_POST['commentId']));
if (!empty($name) && !empty($postComment)) {
$commentQuery = "INSERT INTO `comment`(parent_id,comment,sender)
VALUES('$commentId','$postComment','$name')";
$commentsResult = $db->insert($commentQuery);
if ($commentsResult) {
$msg = "<p class='alert alert-success'><span class='fa-layers
fa-fw'><i class='fas fa-check-circle fa-2x'></i></span>success</p>";
$status = array(
'error' => 0,
'message' => $msg
);
} else {
$msg = "<p class='alert alert-danger'><i class='fas fa-times-
circle fa-2x'></i>deny</p>";
$status = array(
'error' => 1,
'message' => $msg
);
}
} else {
$msg = "<p class='alert alert-warning'><i class='fas fa-bell fa-2x'>
</i>empty field</p>";
$status = array(
'error' => 2,
'message' => $msg
);
}
echo json_encode($status);
}
And display comments with reply is like that
<?php
ini_set('memory_limit', '1024M');
require_once __DIR__.'/../vendor/autoload.php';
include __DIR__.'/../lib/Database.php';
include __DIR__.'/../config/config.php';
include __DIR__.'/../lib/Session.php';
include __DIR__.'/../helpers/Format.php';
$db = new Database();
$fm = new Format();
$commentQuery = "SELECT * FROM `comment` WHERE `parent_id`='0' ORDER BY `id`
DESC";
$result = $db->select($commentQuery);
$commentReply = '';
if ($result) {
while ($cmResult = $result->fetch_assoc()) {
$commentReply .= ' <div class="panel panel-default">
<div class="panel-heading">
<div class="fa-2x text-right" style="border: none;">
<button class=" btn btn-default delClick" id="
'.$cmResult['id'].'">
<span class="fa-layers fa-fw"
style="background:Mistyrose; border: none;">
<i class="fa-inverse fas fa-times-circle"
style="color: black;" data-fa-transform="shrink-3"> </i>
</span>
</button>
</div>
<h4>
<span class= "fa-layers fa-fw">
<i class="fa-inverse fas fa-user-graduate fa-3x" style="
background:MistyRose" >
</i> </span> ' . $cmResult["sender"] . '
</h4>
<p class="date">' . $fm->DateFormat($cmResult["date"]) .
'</p>
</div>
<div class="panel-body">
<p class="comment">' . $fm-
>textShorten($cmResult["comment"], 70) . '</p>
<button type="button" class="btn btn-warning reply"
id="' . $cmResult["id"] . '">REPLY</button>
</div>
</div>';
$commentReply .= getReply($db, $fm, $cmResult['id']);
}
echo $commentReply;
}
function getReply($db, $fm, $parentId = 0, $margin = 0) {
//$fm = new Format();
//$db = new Database();
$commentReply = '';
$getQuery = "SELECT * FROM `comment` WHERE `parent_id`='$parentId'";
$result = $db->select($getQuery);
// $count = mysqli_num_rows($result);
if ($parentId == 0) {
$margin = 0;
} else {
$margin = $margin + 28;
}
if (!empty($result) || $result > 0) {
while ($cmResult = $result->fetch_assoc()) {
// $fm =new Format();
$cmDate = $fm->DateFormat($cmResult['date']);$shorten = $fm-
>textShorten($cmResult['comment'], 70);
$commentReply .= ' <div class="panel_wrapper">
<div class="panel panel-default" style="margin-left: '. $margin. 'px">
<div class="panel-heading">
<div class="fa-2x text-right" style="border: none;">
<button class=" btn btn-default delClick" id=" '.$cmResult['id'].'">
<span class="fa-layers fa-fw" style="background:Mistyrose; border: none;">
<i class="fa-inverse fas fa-times-circle" style="color: black;" data-fa-
transform="shrink-3">
</i>
</span>
</button>
</div>
<h4>
<span class="fa-layers fa-fw">
<i class="fa-inverse fas fa-user-graduate fa-3x"
data-fa-transform="shrink-8"
style="background:MistyRose">
</i>
</span>
'. $cmResult["sender"].'
</h4>
<p class="date">'.$cmDate.'</p>
</div>
<div class="panel-body">
<p class="comment">'. $shorten.'</p>
<button type="button" class="btn btn-warning reply" id="'.
$cmResult['id'].'">
REPLY
</button>
</div>
</div>
</div>';
$commentReply .= getReply($db, $fm, $cmResult['id'], $margin);
}
}
return $commentReply;
}
I submit the comment form using ajax request.It is working fine with success message.
comment.js
$(document).ready(function(){
//$('#all-comments').hide();
showComments();
$('#commentForm').on('submit', function(e){
e.preventDefault();
//showComments().disable();
var formData = $(this).serialize();
// var message = $('#Messages').val();
$.ajax({
url: "../scripts/comment.php",
method: "POST",
data:
formData,
dataType: "JSON",
success:function(response) {
if(!response.error) {
$('#commentForm')[0].reset();
$('#commentId').val('0');
showComments();
$('#message').show().fadeIn('500').html(response.message);
$('#message').show().fadeOut('500').html(response.message);
$('#message').show().fadeIn('500').html(response.message);
$('#message').show().fadeOut('500').html(response.message);
}else if (response.error) {
$('#message').show().fadeIn("500").html(response.message);
$('#message').show().fadeOut("500").html(response.message);
$('#message').show().fadeIn("500").html(response.message);
$('#message').show().fadeOut("500").html(response.message);
}
}
});
//return false;
});
$(document).on('click','.reply', function(){
var commentId = $(this).attr('id');
$('#commentId').val(commentId);
$('#name').focus();
});
});
function showComments() {
$.ajax({
url:"../scripts/disqus.php",
type:"POST",
//dataType: "JSON",
success:function(response) {
// $('#all-comments').show();
$('#showComments').html(response);
}
})
}
I would like to edit delete comment both parent and reply using ajax.I tried many times but can't make it work.If anybody help me to achieve this it will be great. thanks. what i tried for deleting comment as like below
delete.php
<?php
//include __DIR__.'/../lib/Session.php';
include __DIR__.'/../vendor/autoload.php';
include __DIR__.'/../config/config.php';
include __DIR__.'/../lib/Database.php';
$db = new Database();
$delQuery = "DELETE FROM comment WHERE parent_id= 0";
$dResult = $db->delete($delQuery);
/*echo "<pre>";
var_dump($dResult);
echo "</pre>";
die();*/
if ($dResult) {
$msg = "<p class='alert alert-success'>
<i class='fas fa-times-circle fa-2x'>
</i>Comment deleted Successfully!!
</p>";
$status = array(
'error' => 3,
'message' => $msg
);
} else {
$msg = "<p class='alert alert-info'>
<i class='fas fa-warning-circle fa-2x'>
</i>Comment cannot delete.
</p>";
$status = array(
'error' => 4,
'message' => $msg
);
}
echo json_encode($status);
the ajax request for delete query is like that but it didn't work
$(document).on('click','.delClick',function (e) {
e.preventDefault();
// var delId = $(this).attr('id');
deleteComment();
window.location='../Template/Excellent.php';
});
function deleteComment()
{
// var data = $(this).attr('id');
$.ajax({
url:"../scripts/disqus_template.php",
type: "POST",
// data: data,
dataType: "JSON",
success: function (response) {
if(!response.error) {
$('#delmessage').html(response);
$('#delmessage').fadeIn('slow').html(response.message);
}
}
});
return false;
}
php ajax comments sql-delete delete-row
add a comment |
I can Manage comment display with reply. But I have issues about deleting parent comment with reply. You can understand from below code.
This is comment.php file.
<?php
require_once '../vendor/autoload.php';
include "../lib/Database.php";
include "../config/config.php";
include "../helpers/Format.php";
$db = new Database();
$fm = new Format();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = mysqli_real_escape_string($db->link,
$fm->validation($_POST['sender']));
$postComment = mysqli_real_escape_string($db->link,
$fm->validation($_POST['comment']));
$commentId =mysqli_real_escape_string($db->link,
$fm->validation($_POST['commentId']));
if (!empty($name) && !empty($postComment)) {
$commentQuery = "INSERT INTO `comment`(parent_id,comment,sender)
VALUES('$commentId','$postComment','$name')";
$commentsResult = $db->insert($commentQuery);
if ($commentsResult) {
$msg = "<p class='alert alert-success'><span class='fa-layers
fa-fw'><i class='fas fa-check-circle fa-2x'></i></span>success</p>";
$status = array(
'error' => 0,
'message' => $msg
);
} else {
$msg = "<p class='alert alert-danger'><i class='fas fa-times-
circle fa-2x'></i>deny</p>";
$status = array(
'error' => 1,
'message' => $msg
);
}
} else {
$msg = "<p class='alert alert-warning'><i class='fas fa-bell fa-2x'>
</i>empty field</p>";
$status = array(
'error' => 2,
'message' => $msg
);
}
echo json_encode($status);
}
And display comments with reply is like that
<?php
ini_set('memory_limit', '1024M');
require_once __DIR__.'/../vendor/autoload.php';
include __DIR__.'/../lib/Database.php';
include __DIR__.'/../config/config.php';
include __DIR__.'/../lib/Session.php';
include __DIR__.'/../helpers/Format.php';
$db = new Database();
$fm = new Format();
$commentQuery = "SELECT * FROM `comment` WHERE `parent_id`='0' ORDER BY `id`
DESC";
$result = $db->select($commentQuery);
$commentReply = '';
if ($result) {
while ($cmResult = $result->fetch_assoc()) {
$commentReply .= ' <div class="panel panel-default">
<div class="panel-heading">
<div class="fa-2x text-right" style="border: none;">
<button class=" btn btn-default delClick" id="
'.$cmResult['id'].'">
<span class="fa-layers fa-fw"
style="background:Mistyrose; border: none;">
<i class="fa-inverse fas fa-times-circle"
style="color: black;" data-fa-transform="shrink-3"> </i>
</span>
</button>
</div>
<h4>
<span class= "fa-layers fa-fw">
<i class="fa-inverse fas fa-user-graduate fa-3x" style="
background:MistyRose" >
</i> </span> ' . $cmResult["sender"] . '
</h4>
<p class="date">' . $fm->DateFormat($cmResult["date"]) .
'</p>
</div>
<div class="panel-body">
<p class="comment">' . $fm-
>textShorten($cmResult["comment"], 70) . '</p>
<button type="button" class="btn btn-warning reply"
id="' . $cmResult["id"] . '">REPLY</button>
</div>
</div>';
$commentReply .= getReply($db, $fm, $cmResult['id']);
}
echo $commentReply;
}
function getReply($db, $fm, $parentId = 0, $margin = 0) {
//$fm = new Format();
//$db = new Database();
$commentReply = '';
$getQuery = "SELECT * FROM `comment` WHERE `parent_id`='$parentId'";
$result = $db->select($getQuery);
// $count = mysqli_num_rows($result);
if ($parentId == 0) {
$margin = 0;
} else {
$margin = $margin + 28;
}
if (!empty($result) || $result > 0) {
while ($cmResult = $result->fetch_assoc()) {
// $fm =new Format();
$cmDate = $fm->DateFormat($cmResult['date']);$shorten = $fm-
>textShorten($cmResult['comment'], 70);
$commentReply .= ' <div class="panel_wrapper">
<div class="panel panel-default" style="margin-left: '. $margin. 'px">
<div class="panel-heading">
<div class="fa-2x text-right" style="border: none;">
<button class=" btn btn-default delClick" id=" '.$cmResult['id'].'">
<span class="fa-layers fa-fw" style="background:Mistyrose; border: none;">
<i class="fa-inverse fas fa-times-circle" style="color: black;" data-fa-
transform="shrink-3">
</i>
</span>
</button>
</div>
<h4>
<span class="fa-layers fa-fw">
<i class="fa-inverse fas fa-user-graduate fa-3x"
data-fa-transform="shrink-8"
style="background:MistyRose">
</i>
</span>
'. $cmResult["sender"].'
</h4>
<p class="date">'.$cmDate.'</p>
</div>
<div class="panel-body">
<p class="comment">'. $shorten.'</p>
<button type="button" class="btn btn-warning reply" id="'.
$cmResult['id'].'">
REPLY
</button>
</div>
</div>
</div>';
$commentReply .= getReply($db, $fm, $cmResult['id'], $margin);
}
}
return $commentReply;
}
I submit the comment form using ajax request.It is working fine with success message.
comment.js
$(document).ready(function(){
//$('#all-comments').hide();
showComments();
$('#commentForm').on('submit', function(e){
e.preventDefault();
//showComments().disable();
var formData = $(this).serialize();
// var message = $('#Messages').val();
$.ajax({
url: "../scripts/comment.php",
method: "POST",
data:
formData,
dataType: "JSON",
success:function(response) {
if(!response.error) {
$('#commentForm')[0].reset();
$('#commentId').val('0');
showComments();
$('#message').show().fadeIn('500').html(response.message);
$('#message').show().fadeOut('500').html(response.message);
$('#message').show().fadeIn('500').html(response.message);
$('#message').show().fadeOut('500').html(response.message);
}else if (response.error) {
$('#message').show().fadeIn("500").html(response.message);
$('#message').show().fadeOut("500").html(response.message);
$('#message').show().fadeIn("500").html(response.message);
$('#message').show().fadeOut("500").html(response.message);
}
}
});
//return false;
});
$(document).on('click','.reply', function(){
var commentId = $(this).attr('id');
$('#commentId').val(commentId);
$('#name').focus();
});
});
function showComments() {
$.ajax({
url:"../scripts/disqus.php",
type:"POST",
//dataType: "JSON",
success:function(response) {
// $('#all-comments').show();
$('#showComments').html(response);
}
})
}
I would like to edit delete comment both parent and reply using ajax.I tried many times but can't make it work.If anybody help me to achieve this it will be great. thanks. what i tried for deleting comment as like below
delete.php
<?php
//include __DIR__.'/../lib/Session.php';
include __DIR__.'/../vendor/autoload.php';
include __DIR__.'/../config/config.php';
include __DIR__.'/../lib/Database.php';
$db = new Database();
$delQuery = "DELETE FROM comment WHERE parent_id= 0";
$dResult = $db->delete($delQuery);
/*echo "<pre>";
var_dump($dResult);
echo "</pre>";
die();*/
if ($dResult) {
$msg = "<p class='alert alert-success'>
<i class='fas fa-times-circle fa-2x'>
</i>Comment deleted Successfully!!
</p>";
$status = array(
'error' => 3,
'message' => $msg
);
} else {
$msg = "<p class='alert alert-info'>
<i class='fas fa-warning-circle fa-2x'>
</i>Comment cannot delete.
</p>";
$status = array(
'error' => 4,
'message' => $msg
);
}
echo json_encode($status);
the ajax request for delete query is like that but it didn't work
$(document).on('click','.delClick',function (e) {
e.preventDefault();
// var delId = $(this).attr('id');
deleteComment();
window.location='../Template/Excellent.php';
});
function deleteComment()
{
// var data = $(this).attr('id');
$.ajax({
url:"../scripts/disqus_template.php",
type: "POST",
// data: data,
dataType: "JSON",
success: function (response) {
if(!response.error) {
$('#delmessage').html(response);
$('#delmessage').fadeIn('slow').html(response.message);
}
}
});
return false;
}
php ajax comments sql-delete delete-row
I can Manage comment display with reply. But I have issues about deleting parent comment with reply. You can understand from below code.
This is comment.php file.
<?php
require_once '../vendor/autoload.php';
include "../lib/Database.php";
include "../config/config.php";
include "../helpers/Format.php";
$db = new Database();
$fm = new Format();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = mysqli_real_escape_string($db->link,
$fm->validation($_POST['sender']));
$postComment = mysqli_real_escape_string($db->link,
$fm->validation($_POST['comment']));
$commentId =mysqli_real_escape_string($db->link,
$fm->validation($_POST['commentId']));
if (!empty($name) && !empty($postComment)) {
$commentQuery = "INSERT INTO `comment`(parent_id,comment,sender)
VALUES('$commentId','$postComment','$name')";
$commentsResult = $db->insert($commentQuery);
if ($commentsResult) {
$msg = "<p class='alert alert-success'><span class='fa-layers
fa-fw'><i class='fas fa-check-circle fa-2x'></i></span>success</p>";
$status = array(
'error' => 0,
'message' => $msg
);
} else {
$msg = "<p class='alert alert-danger'><i class='fas fa-times-
circle fa-2x'></i>deny</p>";
$status = array(
'error' => 1,
'message' => $msg
);
}
} else {
$msg = "<p class='alert alert-warning'><i class='fas fa-bell fa-2x'>
</i>empty field</p>";
$status = array(
'error' => 2,
'message' => $msg
);
}
echo json_encode($status);
}
And display comments with reply is like that
<?php
ini_set('memory_limit', '1024M');
require_once __DIR__.'/../vendor/autoload.php';
include __DIR__.'/../lib/Database.php';
include __DIR__.'/../config/config.php';
include __DIR__.'/../lib/Session.php';
include __DIR__.'/../helpers/Format.php';
$db = new Database();
$fm = new Format();
$commentQuery = "SELECT * FROM `comment` WHERE `parent_id`='0' ORDER BY `id`
DESC";
$result = $db->select($commentQuery);
$commentReply = '';
if ($result) {
while ($cmResult = $result->fetch_assoc()) {
$commentReply .= ' <div class="panel panel-default">
<div class="panel-heading">
<div class="fa-2x text-right" style="border: none;">
<button class=" btn btn-default delClick" id="
'.$cmResult['id'].'">
<span class="fa-layers fa-fw"
style="background:Mistyrose; border: none;">
<i class="fa-inverse fas fa-times-circle"
style="color: black;" data-fa-transform="shrink-3"> </i>
</span>
</button>
</div>
<h4>
<span class= "fa-layers fa-fw">
<i class="fa-inverse fas fa-user-graduate fa-3x" style="
background:MistyRose" >
</i> </span> ' . $cmResult["sender"] . '
</h4>
<p class="date">' . $fm->DateFormat($cmResult["date"]) .
'</p>
</div>
<div class="panel-body">
<p class="comment">' . $fm-
>textShorten($cmResult["comment"], 70) . '</p>
<button type="button" class="btn btn-warning reply"
id="' . $cmResult["id"] . '">REPLY</button>
</div>
</div>';
$commentReply .= getReply($db, $fm, $cmResult['id']);
}
echo $commentReply;
}
function getReply($db, $fm, $parentId = 0, $margin = 0) {
//$fm = new Format();
//$db = new Database();
$commentReply = '';
$getQuery = "SELECT * FROM `comment` WHERE `parent_id`='$parentId'";
$result = $db->select($getQuery);
// $count = mysqli_num_rows($result);
if ($parentId == 0) {
$margin = 0;
} else {
$margin = $margin + 28;
}
if (!empty($result) || $result > 0) {
while ($cmResult = $result->fetch_assoc()) {
// $fm =new Format();
$cmDate = $fm->DateFormat($cmResult['date']);$shorten = $fm-
>textShorten($cmResult['comment'], 70);
$commentReply .= ' <div class="panel_wrapper">
<div class="panel panel-default" style="margin-left: '. $margin. 'px">
<div class="panel-heading">
<div class="fa-2x text-right" style="border: none;">
<button class=" btn btn-default delClick" id=" '.$cmResult['id'].'">
<span class="fa-layers fa-fw" style="background:Mistyrose; border: none;">
<i class="fa-inverse fas fa-times-circle" style="color: black;" data-fa-
transform="shrink-3">
</i>
</span>
</button>
</div>
<h4>
<span class="fa-layers fa-fw">
<i class="fa-inverse fas fa-user-graduate fa-3x"
data-fa-transform="shrink-8"
style="background:MistyRose">
</i>
</span>
'. $cmResult["sender"].'
</h4>
<p class="date">'.$cmDate.'</p>
</div>
<div class="panel-body">
<p class="comment">'. $shorten.'</p>
<button type="button" class="btn btn-warning reply" id="'.
$cmResult['id'].'">
REPLY
</button>
</div>
</div>
</div>';
$commentReply .= getReply($db, $fm, $cmResult['id'], $margin);
}
}
return $commentReply;
}
I submit the comment form using ajax request.It is working fine with success message.
comment.js
$(document).ready(function(){
//$('#all-comments').hide();
showComments();
$('#commentForm').on('submit', function(e){
e.preventDefault();
//showComments().disable();
var formData = $(this).serialize();
// var message = $('#Messages').val();
$.ajax({
url: "../scripts/comment.php",
method: "POST",
data:
formData,
dataType: "JSON",
success:function(response) {
if(!response.error) {
$('#commentForm')[0].reset();
$('#commentId').val('0');
showComments();
$('#message').show().fadeIn('500').html(response.message);
$('#message').show().fadeOut('500').html(response.message);
$('#message').show().fadeIn('500').html(response.message);
$('#message').show().fadeOut('500').html(response.message);
}else if (response.error) {
$('#message').show().fadeIn("500").html(response.message);
$('#message').show().fadeOut("500").html(response.message);
$('#message').show().fadeIn("500").html(response.message);
$('#message').show().fadeOut("500").html(response.message);
}
}
});
//return false;
});
$(document).on('click','.reply', function(){
var commentId = $(this).attr('id');
$('#commentId').val(commentId);
$('#name').focus();
});
});
function showComments() {
$.ajax({
url:"../scripts/disqus.php",
type:"POST",
//dataType: "JSON",
success:function(response) {
// $('#all-comments').show();
$('#showComments').html(response);
}
})
}
I would like to edit delete comment both parent and reply using ajax.I tried many times but can't make it work.If anybody help me to achieve this it will be great. thanks. what i tried for deleting comment as like below
delete.php
<?php
//include __DIR__.'/../lib/Session.php';
include __DIR__.'/../vendor/autoload.php';
include __DIR__.'/../config/config.php';
include __DIR__.'/../lib/Database.php';
$db = new Database();
$delQuery = "DELETE FROM comment WHERE parent_id= 0";
$dResult = $db->delete($delQuery);
/*echo "<pre>";
var_dump($dResult);
echo "</pre>";
die();*/
if ($dResult) {
$msg = "<p class='alert alert-success'>
<i class='fas fa-times-circle fa-2x'>
</i>Comment deleted Successfully!!
</p>";
$status = array(
'error' => 3,
'message' => $msg
);
} else {
$msg = "<p class='alert alert-info'>
<i class='fas fa-warning-circle fa-2x'>
</i>Comment cannot delete.
</p>";
$status = array(
'error' => 4,
'message' => $msg
);
}
echo json_encode($status);
the ajax request for delete query is like that but it didn't work
$(document).on('click','.delClick',function (e) {
e.preventDefault();
// var delId = $(this).attr('id');
deleteComment();
window.location='../Template/Excellent.php';
});
function deleteComment()
{
// var data = $(this).attr('id');
$.ajax({
url:"../scripts/disqus_template.php",
type: "POST",
// data: data,
dataType: "JSON",
success: function (response) {
if(!response.error) {
$('#delmessage').html(response);
$('#delmessage').fadeIn('slow').html(response.message);
}
}
});
return false;
}
php ajax comments sql-delete delete-row
php ajax comments sql-delete delete-row
edited Jan 20 at 4:50
Ridwanur R khan
asked Jan 19 at 11:33
Ridwanur R khanRidwanur R khan
12
12
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266640%2fphp-ajax-delete-parent-comment-with-comment-reply%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266640%2fphp-ajax-delete-parent-comment-with-comment-reply%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown