我一直在争夺这片的代码小时,没有任何进展。任何人都可以帮助我解决这个问题吗? 我正在使用我网站上的论坛页面,提供对评论功能的评论和回复。 点击每个评论下的回复按钮,textarea会动态显示回复。回复被插入数据库,回复ID和原始评论ID(在回复原始评论时生成的ID)。 我现在的挑战是,答复任何在论坛上的评论似乎只引用最新的评论的ID(可能是因为回复的形式是动态生成)。因此,在我的数据库中,他们拥有最新评论的ID。这实际上意味着取回时,答复将全部排队在最新评论之下。我需要的是引用回复的评论ID的方式。 这里是我的PHP代码:如何使回复评论引用评论的ID在论坛
<?php
require("includes/conn.php");
$stmt=$conn->prepare("SELECT post_id, user, topic, post, time FROM post_tb ORDER BY time DESC");
$stmt->execute();
$result = $stmt->get_result();
$num_of_rows = $result->num_rows;
if ($num_of_rows > 0){
while ($row = $result->fetch_assoc()){
$post_id = $row['post_id'];
$user = $row['user'];
$topic = $row['topic'];
$post = $row['post'];
$time = $row['time'];
$time = date('M dS, Y g:i A', strtotime($time));
echo '<div>
<div>
<h5><strong>'.$user.'</strong></h5><h6>'.$time.'</h6>
<h5><strong>'.ucfirst($topic).'</strong></h5>
<p data-id="'.$post_id.'">'.$post.'</p>
</div>
<div>
<button type="button" class="btn btn-primary rep" id="but_rep" data-id="'.$post_id.'">Reply</button>
</div>
<form id="comment_reply" data-id="'.$post_id.'" method="post" action="">
<input type="text" class="hidden" value="'.$post_id.'" id="post_id">
<input type="text" class="hidden" value="'.$user.'" id="user">
<textarea class="form-control" rows="3" name="post_rep" id="post_rep"></textarea>
<button type="submit" class="btn btn-primary" id="post_rep_sub">Submit</button>
</form>
<div/>';
}
}
?>
我的阿贾克斯/ jQuery的位置:
$("form#comment_reply").hide();//Comment reply form hidden at page load
//To hide reply button and show the reply form
$(document).on('click', 'button.rep', function(e){
e.preventDefault();
var closestDiv = $(this).closest('div');
var closestForm = $(closestDiv).next('form#comment_reply');
$(closestDiv).fadeOut();
$(closestForm).fadeIn();
});
//To process comment reply
$(document).on("click", "form button#post_rep_sub", function(e){
e.preventDefault();
var reply = $("form textarea#post_rep").val();
var name = $("input#user").val();
var post_id = $("input#post_id").val();
if (reply != ''){
$.post('insert_post_reply.php', {'reply':reply, 'name':name, 'post_id':post_id}, function(data){
if (data = 'yes'){
$("form textarea#post_rep").val("");
fetchCommentReply();
}
});
}else{
return false;
}
});
什么是fetchCommentReply();做? – mplungjan
@ mplungjan.thanks.It这么取的答复基础上,评论的ID意见已回复的功能。问题不在于它,因为即使警报(post_id)也不断提醒最新评论的ID。 – banky
请发表[mcve] - 如果您认为这是一个PHP问题,那么我看不到它。如果它只是一个jQuery问题,那么请使用'<>'按钮发布PHP呈现的HTML,这样我们就可以看到是否存在关闭问题 - 这最可能是 – mplungjan