Php Ajax注释系统在具有多个表单时无法正常工作
因此,我做这个网站是为了一个类,我需要使用ajax作为评论系统。到目前为止,我管理用户JavaScript和不保存到数据库的内容,但当我按submit时,它不会显示评论 另外一个问题是,当我有2篇帖子时,它会生成2张表单。其中1个表单无法使用ajax,甚至无法插入到数据库中 index.php代码Php Ajax注释系统在具有多个表单时无法正常工作,php,html,ajax,Php,Html,Ajax,因此,我做这个网站是为了一个类,我需要使用ajax作为评论系统。到目前为止,我管理用户JavaScript和不保存到数据库的内容,但当我按submit时,它不会显示评论 另外一个问题是,当我有2篇帖子时,它会生成2张表单。其中1个表单无法使用ajax,甚至无法插入到数据库中 index.php代码 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="comments.js"></script>
<?php
require_once("menu.php");
$connection = connectToMySQL();
$selectPostQuery = "SELECT * FROM (SELECT * FROM `tblposts` ORDER BY id DESC LIMIT 3) t ORDER BY id DESC";
$result = mysqli_query($connection,$selectPostQuery)
or die("Error in the query: ". mysqli_error($connection));
while ($row = mysqli_fetch_assoc($result))
{
$postid = $row['ID'];
?>
<div class="wrapper">
<div class="titlecontainer">
<h1><?php echo $row['Title']?></h1>
</div>
<div class="textcontainer">
<?php echo $row['Content']?>
</div>
<?php
if (!empty($row['ImagePath'])) #This will check if there is an path in the textfield
{
?>
<div class="imagecontainer">
<img src="images/<?php echo "$row[ImagePath]"; ?>" alt="Article Image">
</div>
<?php
}
?>
<div class="timestampcontainer">
<b>Date posted :</b><?php echo $row['TimeStamp']?>
<b>Author :</b> Admin
</div>
<?php
#Selecting comments corresponding to the post
$selectCommentQuery = "SELECT * FROM `tblcomments` LEFT JOIN `tblusers` ON tblcomments.userID = tblusers.ID WHERE tblcomments.PostID ='$postid'";
$commentResult = mysqli_query($connection,$selectCommentQuery)
or die ("Error in the query: ". mysqli_error($connection));
#renderinf the comments
while ($commentRow = mysqli_fetch_assoc($commentResult))
{
?>
<div class="commentcontainer">
<div class="commentusername"><h1>Username :<?php echo $commentRow['Username']?></h1></div>
<div class="commentcontent"><?php echo $commentRow['Content']?></div>
<div class="commenttimestamp"><?php echo $commentRow['Timestamp']?></div>
</div>
<?php
}
if (!empty($_SESSION['userID']) )
{
?>
<form method="POST" class="post-frm" id="form">
<label>New Comment</label>
<textarea id="comment" name="comment"> </textarea>
<input type="hidden" name="postid" value="<?php echo $postid ?>">
<input id="submit" type="submit" name ="submit" class="button"/>
</form>
<?php
}
echo "</div>";
echo "<br /> <br /><br />";
}
require_once("footer.php") ?>
<?php
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])):
session_start();
include('connection.php');
$connection = connectToMySQL();
$userId = $_SESSION['userID'];
$postId = $_POST['postid'];
$comment = $_POST['comment'];
$insertCommentQuery = "INSERT INTO `tblcomments` (`Content`,`UserID`,`PostID`,`Timestamp`) VALUES ('$comment','$userId','$postId',CURRENT_TIMESTAMP)";
$result = mysqli_query($connection,$insertCommentQuery);
?>
<div class="commentcontainer">
<div class="commentusername"><h1>Username :This is atest</h1></div>
<div class="commentcontent">This is a test</div>
<div class="commenttimestamp">Test</div>
</div>
<?php
connectToMySQL(0);
endif?>
AJAX PHP页面
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="comments.js"></script>
<?php
require_once("menu.php");
$connection = connectToMySQL();
$selectPostQuery = "SELECT * FROM (SELECT * FROM `tblposts` ORDER BY id DESC LIMIT 3) t ORDER BY id DESC";
$result = mysqli_query($connection,$selectPostQuery)
or die("Error in the query: ". mysqli_error($connection));
while ($row = mysqli_fetch_assoc($result))
{
$postid = $row['ID'];
?>
<div class="wrapper">
<div class="titlecontainer">
<h1><?php echo $row['Title']?></h1>
</div>
<div class="textcontainer">
<?php echo $row['Content']?>
</div>
<?php
if (!empty($row['ImagePath'])) #This will check if there is an path in the textfield
{
?>
<div class="imagecontainer">
<img src="images/<?php echo "$row[ImagePath]"; ?>" alt="Article Image">
</div>
<?php
}
?>
<div class="timestampcontainer">
<b>Date posted :</b><?php echo $row['TimeStamp']?>
<b>Author :</b> Admin
</div>
<?php
#Selecting comments corresponding to the post
$selectCommentQuery = "SELECT * FROM `tblcomments` LEFT JOIN `tblusers` ON tblcomments.userID = tblusers.ID WHERE tblcomments.PostID ='$postid'";
$commentResult = mysqli_query($connection,$selectCommentQuery)
or die ("Error in the query: ". mysqli_error($connection));
#renderinf the comments
while ($commentRow = mysqli_fetch_assoc($commentResult))
{
?>
<div class="commentcontainer">
<div class="commentusername"><h1>Username :<?php echo $commentRow['Username']?></h1></div>
<div class="commentcontent"><?php echo $commentRow['Content']?></div>
<div class="commenttimestamp"><?php echo $commentRow['Timestamp']?></div>
</div>
<?php
}
if (!empty($_SESSION['userID']) )
{
?>
<form method="POST" class="post-frm" id="form">
<label>New Comment</label>
<textarea id="comment" name="comment"> </textarea>
<input type="hidden" name="postid" value="<?php echo $postid ?>">
<input id="submit" type="submit" name ="submit" class="button"/>
</form>
<?php
}
echo "</div>";
echo "<br /> <br /><br />";
}
require_once("footer.php") ?>
<?php
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])):
session_start();
include('connection.php');
$connection = connectToMySQL();
$userId = $_SESSION['userID'];
$postId = $_POST['postid'];
$comment = $_POST['comment'];
$insertCommentQuery = "INSERT INTO `tblcomments` (`Content`,`UserID`,`PostID`,`Timestamp`) VALUES ('$comment','$userId','$postId',CURRENT_TIMESTAMP)";
$result = mysqli_query($connection,$insertCommentQuery);
?>
<div class="commentcontainer">
<div class="commentusername"><h1>Username :This is atest</h1></div>
<div class="commentcontent">This is a test</div>
<div class="commenttimestamp">Test</div>
</div>
<?php
connectToMySQL(0);
endif?>
对index.php的更改
...
#renderinf the comments
echo '<div class="comment-block_' . $postid .'">';
while ($commentRow = mysqli_fetch_assoc($commentResult))
{
?>
<div class="commentcontainer">
<div class="commentusername"><h1>Username :<?php echo $commentRow['Username']?></h1></div>
<div class="commentcontent"><?php echo $commentRow['Content']?></div>
<div class="commenttimestamp"><?php echo $commentRow['Timestamp']?></div>
</div>
<?php
}
echo '</div>';
...
我认为您正在尝试将AJAX返回的新HTML附加到
.comment block
中,就像$('.comment block')。append(item)
但是我在你的index.php中没有看到任何注释块
,非常感谢!但是当我使用不同的表单时,我仍然会遇到问题。这意味着只有一个表单可以使用ajax,而另一个则不能。您的ajax代码有点凌乱。抱歉,无意冒犯,但请尝试将数据:form.serialize()
更改为数据:$(此).serialize()
无我知道是这样。以前从未做过AJAX,所以不知道我在做什么:/Now it geitng all message up for exmaple如果我对表单1给出注释,它将进入表单2,如果我对表单2给出注释,它将进入表单1尝试将您的AJAX代码更改为以下答案。希望它能解决问题。不抱歉,还是一样的问题:(如下所示:我在index.php中显示了两个帖子,当我对第一个帖子发表评论时,评论会转到最后一个帖子,当我试图在最后一个帖子上插入评论时,它只是引用页面。你能检查inde.php中的$postId
变量是否带来了正确的帖子ID吗?这是肯定的,因为当omment是插入到数据库中的,它插入正确的postid。哦,是的,然后你需要修改成功:
ajax的一部分和ajax中的一点更改\u php页面什么样的更改?很抱歉,我知道我得到了一点提示。
$(document).ready(function(){
$('#submit').on('click',function(e) {
e.preventDefault();
//send ajax request
var form = $(this).closest('form');
$.ajax({
url: 'ajax_comment.php',
type: 'POST',
cache: false,
dataType: 'json',
data: $(form).serialize(), //form serialize data
beforeSend: function(){
//Changeing submit button value text and disableing it
$(this).val('Submiting ....').attr('disabled', 'disabled');
},
success: function(data)
{
var item = $(data.html).hide().fadeIn(800);
$('.comment-block_' + data.id).append(item);
// reset form and button
$(form).trigger('reset');
$(this).val('Submit').removeAttr('disabled');
},
error: function(e)
{
alert(e);
}
});
});
});