gpt4 book ai didi

javascript - 动态向数据库插入数据

转载 作者:行者123 更新时间:2023-11-29 17:23:53 25 4
gpt4 key购买 nike

 $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 做到这一点。

最佳答案

这段代码应该完全动态地完成你想要的事情

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

关于javascript - 动态向数据库插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51082427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com