如何在ajax调用后在div中显示成功消息

时间:2015-09-03 22:34:31

标签: javascript php jquery html ajax

一直在努力相处,但这里没有答案是我的代码

//在ajax调用后显示注释信息

<div class="cmt-cnt">
    <img src="user_img.jpg" />
        <div class="thecom">
       <h5>Name</h5><span data-utime="time" class="com-dt">time</span>
            <br/>
            <p>
                content
            </p>
        </div>
        </div>

// ajax代码

<script type="text/javascript">
$(document).ready(function(){

  $(".comment-box").keydown(function (e){
     var val_id = $(this);

     if(e.keyCode == 13){
        var I_id = val_id.attr("id");
        var comment =$(val_id).val();
        $.ajax({
                type: "POST",
                url: "ajax/add-comment.php",
                data: 'act=add-com&id_post='+I_id+'&comment='+comment+'&user_sec='+<?php echo $_SESSION['uid'] ?>,
                // from here down did't work well with me
                success: function(html){
                    $(val_id).val('');
                    $('.cmt-cnt').show('fast', function(){
                        $('.cmt-cnt').show('fast');
                        $('.cmt-cnt').before(html);  
                    })
                }  
          });
      }
  });

});
</script>

// ajax / add-comment.php

<?php
extract($_POST);
if($_POST['act'] == 'add-com'):
    $id_post = ($id_post);
    $comment = ($comment);
    $user_sec=($user_sec);

    // Connect to the database
    include('../config.php'); 

    //insert the comment in the database
    mysql_query("INSERT INTO comments (uid, comment, id_post)VALUES( '$user_sec', '$comment', '$id_post')");

    if(!mysql_errno()){
?>

    <div class="cmt-cnt">
    <img src="user_img/me.jpg" />
        <div class="thecom">
            <h5>mars</h5><span data-utime="" class="com-dt">345678</span>
            <br/>
            <p>
                <?php echo $comment; ?>
            </p>
        </div>
        </div>

    <?php } ?>
<?php endif; ?>

我的问题是,我可以在ajax调用之后在div类.cmt-cnt上显示评论文本,任何人请告诉我该怎么做,你的帮助将不胜感激。

0 个答案:

没有答案