我使用带有AJAX的jQuery向帖子添加评论并显示它,例如Facebook。第一次成功,但是第二次却不起作用。
我尝试了类似问题中存在的解决方案,但是根本没有用。
当我第一次执行它时,它可以正常工作,但是第二次,我发现像"website.com/home.php?3=add+comment&addcomment=wqa";
这样的URL
<form class="forme">
<input type="submit" value="add comment" id="'.$commentID.'" name="'.$commentID.'" class="addcomment" >
<input id="addcomment" name="addcomment" type="text" placeholder="add your comment" data="'.$ids.'" >
<br>
</form>
$(".addcomment").click(function() {
var commentid = $(this).attr("id");
var ids = $(this).next().attr("data");
var text = $(this).next().val();
event.preventDefault();
if (text) {
$.ajax({
url: 'reply.php',
method: 'POST',
data: 'text=' + text + '&comment=' + commentid + '&ids=' + ids,
dataType: 'html',
success: function(data) {
$('.hide').html(data);
$(this).next().val("");
},
error: function(html) {
alert("error");
},
});
} else {
return false;
};
});
'''''''''''
<div id="divpost">
<div id="posttitle">00:00:00-2019-07-04 - france- caen- dadi</div>
<div id="textpost">new comment<br>
<a onclick="shcomment(3)" class="pointer"> <!-- I use shcomment() for toggle show hide class="hide" -->
<div id="commentdialog">
<img class="setcomment" src="images/comment.png" />
<div id="nomber" >1</div> <!-- 1 this the number of replys -->
</div><!-- commentdialog -->
</a></div> <!-- textpost -->
</div> <!-- divpost -->
<div id="3" class="hide">
<div id="commentdiv">
<div id="commenttitle">07:41:57- 2019-07-26 - dadi</div> <!-- information of user and date of his comment -->
<div id="textcomment">q</div> <!-- q is reply of dadi -->
</div> <!-- commentdiv -->
<hr>
<form class="forme">
<input type="submit" value="add comment" id="3" name="3" class="addcomment" ><!-- class="addcomment" I use this class for ajaxand jquery -->
<input id="addcomment" name="addcomment" type="text" placeholder="add your comment" data="2" ><br>
</form> </div> <!-- id="3" class="hide" -->
''''''''''''''
// this is reply.php
<?php
require 'include/functions/connect.php';
if(isset($_POST["text"])&& isset($_POST["comment"])&& isset($_POST["ids"]))
{
$date=date("Y-m-d");
$time=date("H:i:s");
$commentID = $_POST["comment"];
$user=$_POST["ids"];
$comment=$_POST["text"];
$sql = " INSERT INTO reply (comment, ids, text, date, time) VALUES ('$commentID', '$user', '$comment', '$date', '$time')";
$conn->query($sql);
// $sql syntax for insert reply into database
// $sql2 syntax for retrive all replys for comment (or post that its id=$commentID)
$sql2="SELECT R.id, R.comment, R.ids, R.text, R.date, R.time, U.username, U.user_id FROM reply as R
INNER JOIN users AS U ON R.ids = U.user_id
AND R.comment ='$commentID' ORDER BY R.date, R.time ";
$result = $conn->query($sql2);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<div id="commentdiv"><div id="commenttitle">';
echo $row["time"]." - ";
echo $row["date"]." - ";
echo $row["username"];
echo '</div>'; // for commenttitle
echo'<div id="textcomment">';
echo $row["text"];
echo '</div> // for textcomment
</div>';// for commentdiv
}
echo '<hr>
<form class="forme">
<input type="submit" value="add comment" id="'.$commentID.'" name="'.$commentID.'" class="addcomment" >
<input id="addcomment" name="addcomment" type="text" placeholder="add your comment" data="'.$row["user_id"].'" ><br>
</form>
</div>';
}
}
?>