PHP Ajax不会返回任何内容

时间:2012-07-14 15:20:11

标签: php jquery html ajax comments

我有一个评论系统,所以当按下提交按钮时它会将评论发送到数据库,然后将其添加到页面上的评论列表中。

的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 个答案:

答案 0 :(得分:3)

  1. 使用echo代替退出,就像Rafael所说。但是你不需要使用die()或exit()等等。清洁代码不需要使用die()或exit()。
  2. 你在数组中使用了$ comment后面的逗号,但逗号后面没有条目(在我的代码中跟随'error' => false
  3. 当您询问mysql_affected_rows是否大于零(http://www.php.net/manual/en/function.mysql-affected-rows.php)时,您可以检查插入是否成功。 mysql_affected_rows返回受INSERT,UPDATE和DELETE影响的行。
  4. 代码:

    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)));
        }
    }
    

答案 1 :(得分:0)

使用echo()代替exit(json.decode ...)

此外,在此回声之后插入die()。