我在数据库中有一个表用于发布帖子,在另一个表中有评论,我想做的是从数据库中获取我的所有帖子,每发布一个帖子,我都提供了一个表单以提交评论。当我尝试使用带有Ajax的此表单发布评论时,它仅适用于一个帖子而不适用于其他帖子。(在此我要提到的是,由于此表单自动随每个帖子填充,因此其文本区域具有相同的ID ,可能是造成错误),但我不知道如何解决此问题?任何帮助或建议,我们将不胜感激。
(注意:我不是专家,而只是学习者。)
这是我的代码:
<?php
echo '<div class="'.$row['post_id'].'">';
$connect = mysqli_connect("localhost", "root", "", "testing") or die("<p><span style=\"color: red;\">Unable to select database</span></p>");
$post_id = $row['post_id'];
$resultt = mysqli_query($connect,"SELECT * FROM tbl_comment WHERE parent_comment_id = '0' AND post_id = '$post_id' ORDER BY comment_id DESC ");
while($rowss = mysqli_fetch_array($resultt, MYSQLI_BOTH))
{
if ($rowss>0){
echo '
<div>
<div>'.$rowss["comment"].'</div>
<div style="font-size: 12px; font-family: serif;">By <b>'.$rowss["comment_sender_name"].'</b> on '.$rowss["date"].'</div>
<div align="right" style="margin-bottom: 19px;"><a href="#" class="reply" id="'.$rowss["comment_id"].'">Reply</a></div>
</div>
';
} else { echo ''; } }
echo '</div>
<form method="POST" class="comment_form">
<div class="form-group"><textarea name="comment_content" class="form-control comment_content" post_id ="'.$row['post_id'].'" placeholder="Enter Comment" rows="2"></textarea></div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-info" value="Publish" onClick="adcomnt()" />
</div>
</form>
<script>
function adcomnt() {
var post_id = $(".comment_content").attr("post_id");
var comment_name = '.$_SESSION['username'].';
var comment_content = $(".comment_content").val();
var comment_id = 0;
$.ajax({
type: "POST",
url: "add_comment.php",
dataType: "json",
data: "post_id=" +post_id+ "&comment_content=" + comment_content+ "&comment_id=" + comment_id+ "&comment_name=" + comment_name,
success: function(data) {
if(data.status == "error"){
alert("Oops, Comment not inserted!");
}
else if(data.status == "success"){
alert("Thank you! comment inserted!");
}},
});
}
</script>
</div>';
?>
答案 0 :(得分:0)
我不确定,但是我认为问题出在JavaScript变量“ post_id”和“ comment_content”,它们看起来未与文本区域链接。
所以您可以尝试这样的事情:
<?php
echo '<div class="'.$row['post_id'].'">';
$connect = mysqli_connect("localhost", "root", "", "testing") or die("<p><span style=\"color: red;\">Unable to select database</span></p>");
$post_id = $row['post_id'];
$resultt = mysqli_query($connect,"SELECT * FROM tbl_comment WHERE parent_comment_id = '0' AND post_id = '$post_id' ORDER BY comment_id DESC ");
while($rowss = mysqli_fetch_array($resultt, MYSQLI_BOTH))
{
if ($rowss>0){
echo '
<div>
<div>'.$rowss["comment"].'</div>
<div style="font-size: 12px; font-family: serif;">By <b>'.$rowss["comment_sender_name"].'</b> on '.$rowss["date"].'</div>
<div align="right" style="margin-bottom: 19px;"><a href="#" class="reply" id="'.$rowss["comment_id"].'">Reply</a></div>
</div>
';
} else { echo ''; } }
echo '</div>
<div class="form-group"><textarea name="comment_content" class="form-control comment_content" id="content_'.$row['post_id'].'" placeholder="Enter Comment" rows="2"></textarea></div>
<div class="form-group">
<input type="submit" id="submit_comment" post_id="'.$row['post_id'].'" name="submit" class="btn btn-info" value="Publish" onClick="adcomnt()" />
</div>
<script>
$(document).ready(function()
{
$( "#submit_comment" ).click(function(e) {
e.preventDefault();
var post_id = $(this).attr("post_id");
var comment_name = '.$_SESSION['username'].';
var comment_content = $("#content_"+post_id).val();
var comment_id = 0;
$.ajax({
type: "POST",
url: "add_comment.php",
dataType: "json",
data: "post_id=" +post_id+ "&comment_content=" + comment_content+ "&comment_id=" + comment_id+ "&comment_name=" + comment_name,
success: function(data) {
if(data.status == "error"){
alert("Oops, Comment not inserted!");
}
else if(data.status == "success"){
alert("Thank you! comment inserted!");
}},
});
});
});
</script>
</div>';
?>
答案 1 :(得分:0)
通过进行一些更改来解决此问题,因为它是循环内的一种形式,为每个元素提供了唯一的ID:
这是更新的代码:
<form method="POST" class="comment_form" id="f'.$post_id.'">
<div class="form-group"><textarea name="comment_content" class="form-control comment_content" id="content_'.$post_id.'" placeholder="Enter Comment" rows="2"></textarea></div>
<div class="form-group">
<input type="submit" id="submit_comment'.$post_id.'" post_id="'.$post_id.'" name="submit" class="btn btn-info" value="Publish" />
</div></form>
<script>
$(document).ready(function(){
$( "#submit_comment'.$post_id.'" ).click(function(e) {
e.preventDefault()
现在它正在按预期运行。