动态地在数据库中插入数据

时间:2018-06-28 12:02:48

标签: javascript php jquery mysql

 $query = "select * from comments t1 inner join users t2 on t1.user_id = t2.UserId where usercomplain_id='$id'";
   $run =mysqli_query($mysqli,$query);
   while($row=mysqli_fetch_array($run))
   {
   $commentid = $row['comment_id'];
   $comment = $row['comment'];
   $username = $row['UserName'];
   $userid1 = $row['UserId'];
   $date = $row['CDate'];
   $ageDate = time_elapsed_string($date);

  ?>

  <div class="jumbotron" style="border:3px solid #2FAB9B; background-color:#68C8C6;">
  <div class="row">
  <div class="col-md-10">
   <?php echo $comment; ?>
   </div>
   <div class="col-md-2">
   <?php echo $ageDate; ?>
   </div>
   </div>
   <br>
    <label>Comment by &nbsp;<a href="profile.php?id=<?php echo $userid1; ?>"><?php echo $username; ?></a></span></label><br>
    <a class="reply" data-role="<?php echo $commentid; ?>">Reply</a>

    <br>
    <br>

    <div style="width:63%; display:none;" class="replyForm" data-role="<?php echo $commentid; ?>">  
    <form method="post">
    <textarea cols="100" rows="4"></textarea><br>
    <br>
    <input type="submit" name="reply" class="btn btn-primary" style="float:right" value="reply">
    </form>
    </div>
  </div>

  <script>


$(document).ready(function(){
    $(".reply").click(function(){ 
        var current = $(this).attr("data-role");

        $('.replyForm[data-role="'+$(this).attr("data-role")+'"]').fadeIn();
    });
});
</script>
   <?php
 if(isset($_POST['reply']))
 {

     echo "<script>alert('$commentid')</script>";
 }
 ?>
   <?php } ?>

这是一个简单的评论系统,每个评论都有一个单击链接的回复链接,显示一个文本框。我想输入对数据库表的评论回复,因此我想获取特定评论的记录。如何使用PHP做到这一点。

1 个答案:

答案 0 :(得分:0)

此代码应完全按照您的意愿进行

<div class="jumbotron comment-container" data-pk="<?php echo $commentid; ?>">
    <div class="row">
        <div class="col-md-10">
            <?php echo $comment; ?>
        </div>
        <div class="col-md-2">
            <em class="text-muted"><?php echo $ageDate; ?></em>
        </div>
        <div class="col-md-12">
          <label>Comment by &nbsp;<a href="profile.php?id=<?php echo $userid1; ?>"><?php echo $username; ?></a></label><br/>
          <button class="btn btn-primary reply">Reply</button>
        </div>
    </div>
</div>

这是JS部分。为了减少while循环中打印的代码,每次都会复制答复表单,并将其附加在需要的地方。

var reply_form = $('<div class="row replyForm-container"><div class="col-md-12">'+
    '<form method="post" class="reply-form">'+
    '<textarea class="form-control" rows="4">Prefilled content</textarea><br>'+
    '<br>'+
    '<button type="submit" name="reply" class="btn btn-primary" style="float:right" >Reply</button>'+
    '</form>'+
    '</div></div>');

$(".reply").click(function(){
    $(this).hide();
    var $container = $(this).closest('.comment-container');
    var pk = $container.data('pk');
    var rf_clone = reply_form.clone();
    rf_clone.find('form').attr('data-pk', pk).data('pk', pk);
    $container.append( rf_clone.hide().fadeIn(1000) );
});

// working with dynamical elements, we need to use delegation here
$(document).on('submit', '.reply-form', function(e){
    e.preventDefault();
    var reply_container = $(this).closest('.replyForm-container');
    var pk = $(this).data('pk');
    var reply = $(this).find('textarea').val();

    console.log('Insert reply "'+reply+'" for comment ID: '+pk);

    $.ajax({
        type: "POST",
        url: 'my_php_handler.php',
        async: false,
        dataType: "json",
        data: {action: 'add-reply', commend_id: pk, reply_text: reply},
        success: function (response) {
            if( response ) {
                reply_container.fadeOut('slow', function(){
                    var btn = reply_container.closest('.comment-container').find('button.reply');
                    $(this).remove(); //will remove the element after fadeOut completes
                    btn.show();
                })
              }
        }
    });
});

检查工作Fiddle(禁用了ajax)