【发布时间】:2017-06-03 10:41:33
【问题描述】:
我已经与这段代码斗争了几个小时,没有任何进展。任何人都可以帮助我解决这个问题吗? 我正在我网站上的一个论坛页面上工作,其中包含 cmets 和对 cmets 功能的回复。 单击每个 cmets 下的回复按钮时,会动态显示一个文本区域以进行回复。回复将插入到数据库中,其中包含回复 id 和原始评论 id(在发布原始评论时生成的 id 已被回复)。 我现在面临的挑战是,对论坛上任何 cmets 的回复似乎只引用了最新评论的 id(可能是因为回复表单是动态生成的)。因此,在我的数据库中,他们拥有最新评论的 ID。这实际上意味着在获取时,回复将全部排在最新评论下。我需要的是一种引用已回复评论的 id 的方法。 这是我的php代码:
<?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/>';
}
}
?>
还有我的 Ajax/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.thanks.这是一个根据回复的评论的 id 获取对 cmets 的回复的函数。问题不在于它,因为即使是 alert(post_id) 也会不断提醒最新评论的 id。
-
请发minimal reproducible example - 如果您认为这是 PHP 问题,那么我看不到它。如果它只是一个 jQuery 问题,那么请使用
<>按钮发布由 PHP 呈现的 HTML,以便我们查看您是否有关闭问题 - 这是最有可能的