2012-07-14 98 views
0

我有一个评论系统,所以当提交按钮被按下它发送到数据库的注释,然后将其添加到网页上的评论列表。PHP阿贾克斯不返回任何

的comments.php

<div id="comments" itemscope itemtype="http://schema.org/UserComments"> 
           <?php do { ?> 
      <div class="comment shadow effect"> 
        <p class="left tip" title="<?php echo $row_getComments['comment_author'];?> Said"> 
        <img class="avatar" src="<?php echo $row_getComments['avatarurl'];?>" /></p> 
          <p class="body right" itemprop="creator"><?php echo $row_getComments['comment_entry'];?></p> 

        <div class="details small"> 
         <span class="blue"><?php echo timeBetween($row_getComments['communt_date'],time());?></span> · <a class="red" href="#" onclick="$(this).delete_comment(<?php echo $row_getComments['comment_id'];?>); return false;">Remove</a> 
        </div> 
       </div> 
          <?php } while ($row_getComments = mysql_fetch_assoc($getComments)); ?> 
          </div> 

    //Add Comment// 
          <div class="add_comment"> 
      <div class="write shadow comment"> 
       <p class="left"> 
        <img class="avatar" src="#" /> 
       </p> 

       <form method="POST" name="addcomment"> 
<p class="textarea right"><input type="hidden" name="username" value="<?php echo $_SESSION['username'];?>" /> 
        <textarea class="left" cols="40" rows="5" name="post_entry"></textarea> 
     <input class="left" value="SEND" type="submit" /> 
        </p> </form> 

      </div> 
      <a onclick="$(this).add_comment(<?php echo $row_getSinglePost['post_id'];?>);return false;" class="right effect shadow" href="#">Add Comment</a> 
     </div> 

ajax.js-这是如何发送和临危信息。我知道提交按钮的作品,因为它是为灰色出来

jQuery.fn.add_comment = function (page_id) { 
var that = $(this); 

that.hide(10, function() { 
    that.prev().show(); 
}); 

that.parent().find('input[type=submit]').click(function() { 
    var value = $(this).prev().val(); 
    if (value.length < 3) { 
     $(this).prev().addClass('error'); 
     return false; 
    } else { 
     var input = $(this); 
     input.prev().attr('disabled', true); 
     input.attr('disabled', true); 
     $.post("ajax.php", { 
      post_id: page_id, 
      comment: value 
     }, function (data) { 
      if (data.error) { 
       alert("Your Comment Can Not Be Posted"); 
      } else { 
       that.parent().prev('.comments').append('<div class="comment rounded5"><p class="left"><img class="avatar" src="' + data.avatar + '" /></p><p class="body right small">' + data.comment + '<br /><div class="details small"><span class="blue">' + data.time + '</span> · <a class="red" href="#" onclick="$(this).delete_comment(' + data.id + '); return false;">Remove</a></div></p></div>'); 
       input.prev().val(''); 
      } 
      input.prev().attr('disabled', false); 
      input.attr('disabled', false); 
     },'json'); 

    } 
    return false; 
}); 

};

ajax.php

require_once('connections/Main.php'); 
$username = $_SESSION['username']; 
mysql_select_db($database_Main); 
function getavatar($username){ 
    $result = mysql_query("SELECT profile_pic FROM `users` WHERE `username` = '$username' LIMIT 1"); 
    $row = mysql_fetch_row($result); 
    return $row[0]; 
} 
    if(isset($_POST['post_id']) and isset($_POST['comment'])){ 
     $post_id = intval($_POST['post_id']); 
     $comment = mysql_escape_string($_POST['comment']); 
     $time = time(); 
     $insertcom = mysql_query("INSERT INTO `blog_comments` (`author`, `post_num`, `comment_entry`, `communt_date`) VALUES ($username, '{$post_id}', '{$comment}', '{$time}')"); 
     if($insertcom){ 
      $id = mysql_insert_id(); 
      exit(json_encode(array(
       'id' => $id, 
       'avatar' => getavatar($username), 
       'time' => timeBetween($time, time()), 
       'comment' => $comment, 
      ))); 
     } 
    } 

此外,如果说查询没有工作,我将如何错误检查此提交错误?

+2

什么不工作是什么“说”和“输入”这是哪里不工作?你的html结构如何? cture看起来像? ajax能够正常返回吗?用Firebug/ChromeDeveloperTools检查json输出是如何的。基本上:学习如何调试狗屎! – worenga 2012-07-14 15:24:41

+0

确定我的ID是返回null,但所有其他的arent母舰一样仍没有ID的工作,虽然,因为它返回它 – kezi 2012-07-14 15:30:24

+0

代码不会在我的PHP代码返回JSON – kezi 2012-07-14 15:35:28

回答

3
  1. 使用像拉斐尔说呼应,而不是退出。但是你不需要使用die()或者exit()等等。干净的代码不需要使用die()或exit()。
  2. 你用你的数组内$注释后面的逗号,但没有进入后面的逗号(在我的代码的'error' => false如下:
  3. 您可以检查是否插入是成功的,当你问,如果mysql_affected_rows大于零(http://www.php.net/manual/en/function.mysql-affected-rows.php)mysql_affected_rows返回所影响INSERT,UPDATE和DELETE行

代码:??

if($insertcom){ 
    //Check if INSERT was successfully 
    if(mysql_affected_rows() > 0) { 
     $id = mysql_insert_id(); 
     echo(json_encode(array(
      'id' => $id, 
      'avatar' => getavatar($username), 
      'time' => timeBetween($time, time()), 
      'comment' => $comment, //here is the , 
      //Added line to state if error 
      'error' => false 
     ))); 
    } else { 
     //Echo to return error when INSERT was unsuccessful 
     echo(json_encode(array('error' => true))); 
    } 
} 
+0

在谷歌我得到不能读财产错误为空 – kezi 2012-07-14 16:04:07

+0

在谷歌?尝试使用真与假的引号:''true''false'。在JavaScript中,你需要用==来比较“真实” – WolvDev 2012-07-14 16:15:53

+0

在调试它说:“不能读取属性‘错误’空的 – kezi 2012-07-14 16:21:40

0

使用echo(),而不是出口(json.decode ...)

此外,插入此波之后死()。

+0

把回声不工作 – kezi 2012-07-14 15:42:22