gpt4 book ai didi

php - 使用 ajax 的 CodeIgniter 回复仅适用于第一个数据

转载 作者:行者123 更新时间:2023-12-01 04:41:05 25 4
gpt4 key购买 nike

这是我的 Ajax,我想要的是将回复发送到某个评论,但它仅适用于插入数据库的最后一条评论。我认为模型和 Controller 工作得很好。

$(document).ready(function() {
$('#btn_ReplyComment').click(function(){
var commentReply = $('#commentReply').val();
var ReplyDate = $('#ReplyDate').val();
var commentID = $('#commentID').val();

$.ajax({
type:'POST',
data: {commentReply: commentReply, ReplyDate: ReplyDate, commentID: commentID},
url: '<?php echo site_url('Isidran/Reply_Comment'); ?>',
})
})
});

和我的模态

    <?php foreach ($showComment as $row): ?>
<div>
<h4><?php echo $row['username'].":" ?> </h4>
<?php echo $row['comDate'] ?><br />
<?php echo $row['comment'] ?><br />

//button to trigger modal
<button id="com_btn_2" class="btn btn-success btn-sm" data-toggle="modal" data-target="#myModal1<?php echo $row['commentID'] ?>">reply</button>
</div>
<hr> </hr>

//modal to reply into comment only works on the last entered comment
<!-- Modal -->
<div id="myModal1<?php echo $row['commentID'] ?>" class="modal fade" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Reply to this Comment?</h4>
</div>
<div class="modal-body">
<h3><p><?php echo $row['comment'] ?></p></h3>
<h5>Reply:</h5>
<?php $date = date('Y-m-d G:i:s'); ?>
<textarea class="form-control" style="resize:none;" id="commentReply" name="commentReply" maxlength="160" rows="5" cols="50"></textarea>
<input type="hidden" id="ReplyDate" value="<?php echo $date; ?>">
<input type="hidden" id="commentID" value="<?php echo $row['commentID']; ?>">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger" data-dismiss="modal" id="btn_ReplyComment">Send</button>

</div>
</div>

</div>
</div>
<?php endforeach; ?>

Controller :

public function Reply_Comment(){

$this->Blog_model->login();
if($this->session->userdata('userID')){

$userID =$this->session->userID;
$username =$this->session->username;
$reply = $this->input->post('commentReply',true);
$commentID = $this->input->post('commentID',true);
$ReplyDate = $this->input->post('ReplyDate',true);

$data['replyComment']=$this->Blog_model->replyComment($commentID, $userID, $username, $reply, $ReplyDate);

} else {

redirect('CodeSmart/loginFail' , 'refresh');
}
}

型号:

public function replyComment($commentID, $userID, $username, $reply, $ReplyDate){

if($commentID && $userID && $username && $reply && $ReplyDate){

$query = $this->db->query("INSERT INTO `blog`.`tbl_reply` (comment_id, reply_user_id, reply_username, reply, replydate) VALUES ('$commentID', '$userID', '$username', '$reply', '$ReplyDate')");

}
}

最佳答案

是的,您的 Controller 和型号没有问题。问题在于 foreach 中您正在循环该模型。有很多同名的 id,例如 ReplyDate、commentID 等。所以它们没有区别。所以在 jquery 中它取第一个值。为了避免这个问题。您可以使用如下代码在你看来

<?php foreach ($showComment as $row): ?>
<div>
<h4><?php echo $row['username'].":" ?> </h4>
<?php echo $row['comDate'] ?><br />
<?php echo $row['comment'] ?><br />

//button to trigger modal
<button id="com_btn_2" class="btn btn-success btn-sm" data-toggle="modal" data-target="#myModal1<?php echo $row['commentID'] ?>">reply</button>
</div>
<hr> </hr>

//modal to reply into comment only works on the last entered comment
<!-- Modal -->
<div id="myModal1<?php echo $row['commentID'] ?>" class="modal fade" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Reply to this Comment?</h4>
</div>
<div class="modal-body">
<h3><p><?php echo $row['comment'] ?></p></h3>
<h5>Reply:</h5>
<?php $date = date('Y-m-d G:i:s'); ?>
<textarea class="form-control" style="resize:none;" id="commentReply<?php echo $row['commentID'] ?>" name="commentReply" maxlength="160" rows="5" cols="50"></textarea>
<input type="hidden" id="ReplyDate<?php echo $row['commentID'] ?>" value="<?php echo $date; ?>">
<input type="hidden" id="commentID<?php echo $row['commentID'] ?>" value="<?php echo $row['commentID']; ?>">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger" data-dismiss="modal" id="btn_ReplyComment" onclick="add_comment(<?php echo $row['commentID'] ?>); ">Send</button>

</div>
</div>

</div>
</div>
<?php endforeach; ?>

还有你的脚本

<script type="text/javascript">
function add_comment(id) {
var commentReply = $('#commentReply'+id).val();
var ReplyDate = $('#ReplyDate'+id).val();
var commentID = $('#commentID'+id).val();

$.ajax({
type:'POST',
data: {commentReply: commentReply, ReplyDate: ReplyDate, commentID: commentID},
url: '<?php echo site_url('Isidran/Reply_Comment'); ?>',
})
}
</script>

它将按您的预期工作。

关于php - 使用 ajax 的 CodeIgniter 回复仅适用于第一个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37615065/

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