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

时间:2016-06-03 12:59:03

标签: php jquery ajax codeigniter modal-dialog

这是我的Ajax,我想要的是将回复发送到某个注释,但它只适用于插入数据库的最后一个注释。我认为模型和控制器工作正常。

$(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; ?>

控制器:

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')");

        }
    }

1 个答案:

答案 0 :(得分:1)

是的,您的控制器和型号没有问题。问题在于你正在循环这个模型。有很多具有相同名称的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>

它会按预期工作。