I have comment site, where user can comment on comment (subcomment)
Main Comment 1
Sub Comment nr1
Sub Comment nr2
Main Comment 2
Sub Comment nr1
Every Main comment have form that shows on click. When user submits comment or subcomment, message that says "Successful!" is shown in div #result.
This is JS
$(function(){
//reply comment hide>show
$('.reply-comment').on('click', function(e){
e.preventDefault();
$(this).next('.reply-form').show();
});
//ajax to comment.php
$('form').on('submit', function (e) {
$.ajax({
type: 'post',
url: 'comment.php',
data: $(this).serialize(),
success: function () {
$("#result").addClass("alert alert-success").html("Successful");
window.setTimeout(function(){location.reload()},3000);
}
});
e.preventDefault();
});
});
It works but message is shown on 1st occurrence of #result, that's on 1st form at top of page.Is it possible to display message only above form that user comments on ?
This is HTML where forms are dynamically generated from DB for every Main comment
<a href="" class="reply-comment""> Reply </a>
<form action="comment.php" method="post" class="reply-form">
<div id="result"></div>
Name: <input type="text" name="name"><br>
Comment: <input type="text" name="comment"><br>
<input type="hidden" name="parrent" value="<?=$data[$id]["id"]?>">
<input type="hidden" name="comment_post_id" value=<?=$post_id?>>
<input type="submit">
</form>
And comment.php that's called, (SQL Injection not important)
$name = $_POST['name'];
$comment = $_POST['comment'];
$parrent = $_POST['parrent'];// Hiden!
$post_id = $_POST['comment_post_id']; // Hiden!
if ($parrent==="zero"){ //main comment
$sql = "INSERT INTO ...";
}
if ($parrent>0){ //sub comment
$sql = "INSERT INTO ...)";
}
$conn->query($sql)
Main goal is to show successful msg for example if user commented on Main comment 2
Main Comment 1
Sub Comment nr1
Sub Comment nr2
Main Comment 2
Successful! //hides on reload
Sub Comment nr1
Sub Comment nr2