簡體   English   中英

PHP Ajax不返回任何內容

[英]PHP Ajax Doesn't Return Anything

我有一個評論系統,因此當按下“提交”按鈕時,它將評論發送到數據庫,然后將其添加到頁面上的評論列表中。

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

另外,如果查詢不起作用,我將如何對此進行錯誤檢查以提交錯誤?

  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影響的行。

碼:

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM