【问题标题】:Ajax loaded comment not workingAjax 加载的注释不起作用
【发布时间】:2014-11-14 01:50:51
【问题描述】:

我的 php 文件循环出我数据库中的每篇博客文章并创建 cmets 部分。博客帖子很棒。当我对最近的博客文章发表评论时,评论没有出现,但似乎随着空间的扩大增加了一行,只是不包括评论。当我对第一篇文章发表评论时,它工作得很好,完全符合预期。我不知道如何纠正它。我认为这是在评论块选择器中添加一个 .closest 的问题,但这似乎没有奏效。

感谢任何帮助或反馈!

PHP/HTML

<?php   // retrive post
     include('php/config.php');
    include ('php/function.php');
    dbConnect();

    $blog_query = mysql_query(
    'SELECT * 
    FROM Blog 
    ORDER BY DATE DESC');

    $date = date_create($row['DATE']);
    while($row = mysql_fetch_array($blog_query)): ?>

    <div class="post">
        <h2><?php echo $row['TITLE']?></h2>
        <h3><?php echo date_format($date, 'l, F j, Y')?></h3>
        <p><?php echo $row['CONTENT']?></p>
    </div>
    <h2>Comments.....</h2>
    <div class="comment-block">

<?php   // retrieve comments with post id
    $comment_query = mysql_query(
        "SELECT *
        FROM Comments
        WHERE BID = {$row['ID']}
        ORDER BY CID DESC
        LIMIT 15") or die(mysql_error());

        while($comment = mysql_fetch_array($comment_query)):?>

        <div class="comment-item">
            <div class="comment-post">
                <h3><?php echo $comment['UNAME'] ?> <span>said....</span></h3>
                <p><?php echo $comment['COMMENT']?></p>
            </div>
        </div>
    <?php endwhile?>
    </div>

    <h2>Submit new comment</h2>
    <!--comment form -->
    <form id="form" method="post"> 
        <div>
            <input type="hidden" name="BID" value="<?php echo $row['ID']?>">
            <label> <span>Display Name: *</span>
                <input id="uname" type="text" tabindex="1" name="commentName" required />
            </label>
        </div>
        <div>
            <label> <span>Comment: *</span>
                <textarea id="textarea" placeholder="Enter your comment here..." tabindex="2" name="commentMessage" required></textarea>
            </label>
        </div>
        <div>
            <input type="submit" id="submit" value="Submit Comment">
        </div>
    </form>     


<?php endwhile?>    
</div>

jQuery/Ajax:

var form = $('form');
var submit = $('#submit');

form.on('submit', function(e) {
    // prevent default action
    e.preventDefault();
    // send ajax request
    $.ajax({
        url: 'php/ajaxcomment.php',
        type: 'POST',
        cache: false,
        data: form.serialize(), //form serizlize data
        beforeSend: function(){
            // change submit button value text and disabled it
            submit.val('Submitting...').attr('disabled', 'disabled');
        },
        success: function(data){
            // Append with fadeIn see http://stackoverflow.com/a/978731
            var item = $(data).hide().fadeIn(800);
            $('.comment-block').append(item);

            // reset form and button
            form.trigger('reset');
            submit.val('Submit Comment').removeAttr('disabled');
        },
        error: function(e){
            alert(e);
        }
    });
});

ajajcomment.php

<?php
if (isset( $_SERVER['HTTP_X_REQUESTED_WITH'] )):
include('config.php');
include('function.php');
dbConnect();

if (!empty($_POST['commentName']) AND !empty($_POST['commentMessage']) AND !empty($_POST      ['BID'])) {
    $name = mysql_real_escape_string($_POST['commentName']);
    $comment = mysql_real_escape_string($_POST['commentMessage']);
    $BID = mysql_real_escape_string($_POST['BID']);

    mysql_query("
        INSERT INTO Comments
        (UNAME, BID, COMMENT)
        VALUES('{$name}', '{$BID}', '{$comment}')");            
}
?>

 <div class="comment-item">
<div class="comment-post">
    <h3><?php echo $name ?> <span>said....</span></h3>
    <p><?php echo $comment ?></p>
</div>
</div>

<?php
dbConnect(0);
endif?>

【问题讨论】:

    标签: javascript php jquery ajax forms


    【解决方案1】:

    当您发表评论时,“php/ajaxcomment.php”会返回什么?纯 HTML?

    我会让“php/ajaxcomment.php”返回 JSON,例如:

    <?php
    /*
    here you do what ever you do now,
    Insert the new comment to database, etc
    */
    
    // Then you return the inserted data:
    $data = array(
      'UNAME' => $username,
      'COMMENT' => $comment,
    );
    
    header('Content-Type: application/json');
    print json_encode( $data );
    ?>
    

    ..并更改 ajax:

    ...
    ...
    success: function(data){
        var commentItem = $('<div/>').addClass('comment-item');
        var commentPost = $('<div/>').addClass('comment-post');
        var user        = $('<h3/>').html( data.UNAME +'<span>said...</span>' );
        var comment     = $('<p/>').html( data.COMMENT );
    
        commentItem.html( commentPost.html( user + comment ) ).hide().fadeIn(800);
    
        $('.comment-block').append(commentItem);
    
        // reset form and button
        form.trigger('reset');
        submit.val('Submit Comment').removeAttr('disabled');
    },
    ...
    ...
    

    【讨论】:

    • 我会使用 而不是 并且在 javascript 中你应该处理这个
    • Kirbo,感谢您的回复。我编辑了我的代码以反映您的建议,但遇到了同样的问题。我想不通,奇怪的是,在提交时,第二个博客 cmets 甚至没有进入数据库,但第一个博客工作正常。
    • 你能不能也显示“php/ajaxcomment.php”文件?如果没有,请尝试使用 Chrome Web Inspector 或 Firefox Firebug 或类似的工具对其进行调试,以显示发出的请求和得到的响应。添加到“php/ajaxcomment.php”的开头,例如: ';打印_r($_POST);死; ... ... 或:
    • 添加了 php 文件。我明天必须回到这个话题。非常感谢您的帮助!
    • 仅适用于第一个博客/评论部分。在第二个博客实例上,它不会发布到数据库或屏幕。
    猜你喜欢
    • 2011-05-30
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 2020-10-09
    • 2012-09-19
    • 2015-09-14
    相关资源
    最近更新 更多