Ajax / jQuery评论系统 - php

使用Ajax和JQuery构建注释系统,我希望添加注释后重新加载注释所在的div。它发布就好了。到目前为止,这就是我所拥有的。

函数getComments查询数据库并生成html

$.ajax({
type: "POST",
url: "post_comment.php",
data: dataString,
cache: false,
success: function(html){

????????? What should go here... it is a div with id#commentBody

}

<div id="commentBody">
    <ul>
    <?php
        $q = "SELECT * FROM comment WHERE parent_id = 0 AND idpost = $postID";
        $r = mysql_query($q);
        while($row = mysql_fetch_assoc($r)):
            getComments($row,$postID,$custID);
        endwhile;
    ?>
    </ul>
</div> 

参考方案

发布时,应从服务器端脚本返回所需的数据。然后,您可以使用.html() jQuery函数更新div。

因此,例如:

$('#commentBody').html(html);

您还可以只返回最新的注释(可选地作为JSON对象),然后仅使用.append()方法将其添加到#commentBody中。

我将创建一个具有status属性和data属性的JSON对象。当status为-1(或其他值)时,添加注释时出错,您可以在data属性中添加一条消息。 status为0时,表示成功,并且data属性中将提供最新的注释信息。

的PHP

//Check postback variables, add comment and retrieve
//  comment information (such as ID) if necessary
if (postedsuccessfully) {
   $ary = array("status" => 0,
                "data" => array("id" => $commentidvar,
                                "user" => $commentuser,
                                "text" => $comment)
               );
   echo json_encode($ary);
} else {
   $ary = array("status" => -1,
                "data" => "There was a problem adding your comment.");
   echo json_encode($ary);
}

的JavaScript

success: function(json){
   if (json.status == 0) {
      $mydiv = $('<div>');//construct your comment div using json.data.id,
                          //json.data.user, and json.data.text
      $('#commentBody').append($mydiv);
   } else {
      alert(json.data);
   }
}

jQuery Ajax PHP重定向到另一个页面 - php

JavaScript文件:$.ajax({ type: "POST", url: "ajax.php", data: dataString, success: function(r) { $("#div").html(r); } }); 我想在成功的情况下将页面重定向到new.php,所以在我使用a…

AJAX实时电子邮件验证(PHP) - php

当用户在我的网站上创建帐户时,我希望对照我的数据库(MySQL)中的所有当前用户名检查输入的用户名,并确认是否可用。有谁知道我可以使用的任何好的库,或者是jQuery的插件? 参考方案 所需的确切信息以示例形式here给出。它使用jQuery作为JavaScript库

jQuery ajax()返回json对象,但未正确警告 - php

为什么无法使用data [0] .id返回ID? $(document).ready(function(){ $.ajax({ type: 'POST', dataType: "json", url: '<?php echo matry::base_to('tests/map_it'…

jQuery自动完成ui与json问题 - php

我正在尝试使jquery自动完成ui正常工作,但没有成功。没有找到匹配的列表。我收到错误“ TypeError:this.source不是函数”到目前为止,我有一个输入字段<input type="text" id="searchbar" name="title" placeholder=&#…

Ajax Onchange在foreach循环内 - php

我有ajax功能function price(id,prid,divid,key,name) { //alert(id); alert(prid); alert(name); $.ajax({ type: "POST", data: "aid="+id+"&prid="+prid, url:…