Php 通过AJAX发布时,注释显示两次,但在页面重新加载时仅显示一次

Php 通过AJAX发布时,注释显示两次,但在页面重新加载时仅显示一次,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,我使用以下代码向帖子添加注释,并在不刷新页面的情况下显示它,但不知何故,它将注释显示两次而不是一次,但在数据库中只保存一次。当我刷新页面时,每个评论只显示一次,所以我想问题在于“ajax响应”。这是代码,谢谢你的帮助 JavaScript: function addComment(pid) { var url; var comment = document.getElementById("comment").value; xml_http = getXmlHttpObje

我使用以下代码向帖子添加注释,并在不刷新页面的情况下显示它,但不知何故,它将注释显示两次而不是一次,但在数据库中只保存一次。当我刷新页面时,每个评论只显示一次,所以我想问题在于“ajax响应”。这是代码,谢谢你的帮助

JavaScript:

function addComment(pid) {
    var url;
    var comment = document.getElementById("comment").value;
    xml_http = getXmlHttpObject();
    if (xml_http == null) return alert("Your browser is too old to run this page!");
    url = '../auth.php?comment=' + comment + '&pid=' + pid;
    url += '&p=' + Math.random();
    xml_http.onreadystatechange = commentStatus;
    xml_http.open("GET", url, true);
    xml_http.send(null);
}

function commentStatus() {
    $("#comm").append(xml_http.responseText);
}
PHP:

include_once('include/comment.php');
addComment($_GET['pid'],filter($_GET['comment']);
if(空($_会话['pic'])$pic=“images/silh.gif”;
else$pic=“/profile/image.php/{$\u SESSION['uname']}.{$\u SESSION['pic']}?width=45&;cropratio=1:1&;image=/profile/pix/{$\u SESSION['uname']}.{$\u SESSION['pic']}”;
回显“\n”
.""
“通过{$\u会话['uname']}
\n” “立即发布\n” .“

{$\u GET['comment']}

” . “
\n”;

我故意不从数据库中读取注释,我只是将其发回页面。

您可能应该仅在xml\u http状态为4时追加注释。试着这样做:

function addComment(pid) {
    var url;
    var comment = document.getElementById("comment").value;
    var xml_http = getXmlHttpObject();

    if (xml_http == null)
         return alert("Your browser is too old to run this page!");

    url = '../auth.php?comment=' + comment + '&pid=' + pid;
    url += '&p=' + Math.random();

    xml_http.onreadystatechange = function() {
        if (xml_http.readyState==4) {
            appendComment(xml_http.responseText);
        }
    };

    xml_http.open("GET", url, true);
    xml_http.send(null);
}

function appendComment(commentHTML) {
    $("#comm").append(commentHTML);
}

因为没用的头衔而被否决了。修改的标题:)您正在使用jQuery;为什么不使用它的Ajax功能呢?顺便说一句,可能找到:将用户数据输出到网页时,始终使用
htmlspecialchars
encodeurl
(取决于用例)。顺便说一句,您使用的是全局变量,没有任何必要(
xml\u http
)。请不要!并使用
encodeURIComponent
对查询参数进行正确编码。也许你不应该使用
GET
请求来更新你的数据库,
GET
请求应该是幂等的……当你这样做的时候,请去掉全局变量并检查
xml\u http.status
。好吧,我们可以不同意这一点,但我认为,如果有人能给出一个比提问者要求的答案更能说明问题,但又能防止他们犯其他错误,那么就应该这样做。否则,Chibuzo很快就会回来问“为什么我的
responseText
是空的?”或者“为什么我的
xml\uhttp
变量发生了变化?”我更喜欢只回答实际被问过的问题,而不是让那些可能还不知道什么是全局变量以及如何避免使用它的人迷失方向;)我已经这么想了。但我担心这里有很多人只是抄袭粘贴糟糕的教程,而不阅读好的材料。我认为,在尝试AJAX请求之前,应该先学习使用JavaScript这样的语言,因为在AJAX请求中,各种事情都可能出错。我甚至在这里看到了一个关于这样一个没有回调处理程序的请求的问题。但是我同意你的态度,这不值得大讨论,更不用说让我们彼此发疯了。@all,我已经本地化了xml_http变量,但是当我从xml_http_onReadyStateChange()函数调用它时,我得到一个错误,变量未定义。我尝试使用此代码代替$(“#frmCom”).submit(函数(){$.post(../auth.php)”,$(“#frmCom”).serialize(),函数(数据){$(“#comm”).append(xml#http.responseText);};返回false;});我相应地修改了表单,但代码什么也没做。请问它怎么了?非常感谢。
function addComment(pid) {
    var url;
    var comment = document.getElementById("comment").value;
    var xml_http = getXmlHttpObject();

    if (xml_http == null)
         return alert("Your browser is too old to run this page!");

    url = '../auth.php?comment=' + comment + '&pid=' + pid;
    url += '&p=' + Math.random();

    xml_http.onreadystatechange = function() {
        if (xml_http.readyState==4) {
            appendComment(xml_http.responseText);
        }
    };

    xml_http.open("GET", url, true);
    xml_http.send(null);
}

function appendComment(commentHTML) {
    $("#comm").append(commentHTML);
}