使用ajax和php在帖子上评论系统 [英] Commenting system on posts using ajax and php
问题描述
这是我的代码,我将 comment
和post id(隐藏的输入标签)传递给另一个php文件 comments.php
它应该把它插入到数据库中,并显示结果,但它没有发生。
这是我的表单 - 用echo引用的代码的一部分...我有与每篇文章相关联的自动增量 pid
对每个评论都是如此, comment
和 pid
存储在数据库中:
This is my code where I pass the comment
and the post id (hidden input tag) to another php file comments.php
which should insert it into db and show the result but its not happening.
This is my form - part of code quoted in echo...I have an auto increment pid
associated with each post so for every comment, the comment
and the pid
are stored in the database:
JavaScript - 一个函数 comment()
是使用 onsubmit
来请求每个帖子的评论表单。
<form method=\"POST\" onSubmit=\"comment(); return false;\">
<input id=\"comment\" type=\"text\" placeholder=\"Add Comment...\" name=\"comment\">
<input type=\"hidden\" name=\"pid\" value=\"".$row['pid']."\">
<div class=\"z\"><input type=\"submit\" name=\"submit\" ></div>
</form>
JavaScript - a function comment()
is called for each post's comment form using onsubmit
.
这是 comments.php $ c
This is the comments.php
code:
What comments.php
应该返回显示在 index.php
中的 div
中,其中 id = #postid $
<?php
session_start();
include 'db.php';
$j =$_POST['comment'];
$k = $_POST['pid'];
$l =$_SESSION['uname'];
$sql = "INSERT INTO comments (pid,name,comment) values ('$k','$l','$j')";
$r = $conn->prepare($sql);
$r->execute();
if($r) {
echo '<div class="comment">
<a class="avatar">
<img style="height:30px;"src="zmf.jpg">
</a>
<div class="content">
<a class="author">'.$l.'</a>
<div class="metadata">
<span class="date">Today at 5:42PM</span>
</div>
<div class="text">';
echo $j.'</div>
<div class="actions">
</div>
</div>
</div>
';
}
?>
What comments.php
should return would be shown in the div
in the index.php
having an id = #postid
which is already assigned in that index.php for each post:
index.php
有这样一个<$每个帖子的c $ c> div ,以便完成评论的帖子,评论将显示在每个帖子下方的 div
中。任何帮助将不胜感激。
<div id=\"".$row['pid']."\">
</div>
我运行两个while循环来显示每篇文章的所有previuos评论。
第一个循环显示post和一个嵌套while循环,每个帖子显示每个帖子的注释。每个帖子在while循环中的注释形式以及具有id = postid的div以显示ajax结果。
index.php
has such a div
for each post so the post on which a comment is done, comment will be shown in that div
placed below each post. Any help would be appreciated.
i run two while loop to show all previuos comments for each post like this.
first loop shows post and a nested while loop with each post that shows comment on each post..and a comment form for each post in while loop along with a div having an id = postid to show the ajax result..
#nested评论循环
$ zmf =选择*从注释WHERE pid ='。 $ row ['pid']。 'ORDER BY comid DESC;
$ zed = $ conn-> prepare($ zmf);
$ zed-> execute();
$ run = $ zed-> fetch();
while($ run = $ zed-> fetch())
{
my div code and all..for comments
}
}
$q = $conn->prepare("SELECT * FROM posts ORDER BY pid DESC");
$q->execute();
while($row = $q->fetch(PDO::FETCH_ASSOC)) {
#my post in div
#comment form
<form method=\"POST\" onsubmit=\"comment()\">
<input id=\"comment\" type=\"text\" placeholder=\"Add Comment...\" name=\"comment\">
<input type=\"hidden\" name=\"pid\" value=\"".$row['pid']."\">
<div class=\"z\"><input type=\"submit\" name=\"submit\" ></div>
</form>
#div where recent comment is shown using ajax
<div id=\"".$row['pid']."\">
</div>
#nested while loop for comments
$zmf = "SELECT * FROM comments WHERE pid = '" . $row['pid'] . "' ORDER BY comid DESC";
$zed = $conn->prepare($zmf);
$zed->execute();
$run = $zed->fetch();
while($run = $zed->fetch())
{
my div code and all..for comments
}
}
每个帖子都有一个id pid ..
,但是它的nt工作..plz帮助
every post has an id pid .. but its nt working ..plz help
推荐答案
页面上是否有多个< form>
元素?如果是这样,尝试更改 $('form')。serialize()
to $(this).serialize()
。
Do you have multiple <form>
elements on the page? If so, try changing $('form').serialize()
to $(this).serialize()
.
你能提供更多关于你得到的结果的信息吗?你在数据库中看到任何条目吗?在ajax请求之后,您在div中看到的输出是什么?
Can you provide a little more information on what result you are getting? Are you seeing any entry in the database? What is the output that you are seeing in the div, after the ajax request?
您使用的是PDO吗?如果($ r)检查并添加 print_r($ r-> errorInfo())
>。尝试在Firefox中的Chrome / Firebug中启用开发人员工具面板,并检查ajax(XHR)请求的结果。如果sql查询失败,可能会引发 PDOException
。确保XHR请求返回 200 OK
http状态。
Are you using PDO? Add an else condition to your if($r)
check and add print_r($r->errorInfo())
. Try enabling the developer tools panel in Chrome / Firebug in Firefox and check the result of the ajax (XHR) request. If the sql query is failing, it might raise a PDOException
. Ensure that the XHR request is returning 200 OK
http status.
请尝试以下代码并发布输出
Try the following code and post the output
<?php
session_start();
include 'db.php';
$j = $_POST['comment'];
$k = $_POST['pid'];
$l = $_SESSION['uname'];
$sql = "INSERT INTO comments (pid,name,comment) values ('$k','$l','$j')";
print_r($_POST);
try {
$r = $conn->prepare($sql);
$r->execute();
if ($r) {
echo '<div class="comment">
<a class="avatar">
<img style="height:30px;"src="zmf.jpg">
</a>
<div class="content">
<a class="author">' . $l . '</a>
<div class="metadata">
<span class="date">Today at 5:42PM</span>
</div>
<div class="text">' . $j . '</div>
<div class="actions">
</div>
</div>
</div>
';
} else {
print_r($r->errorInfo());
}
} catch (\Exception $e) {
print_r($e);
}
这篇关于使用ajax和php在帖子上评论系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!