成功:使用codeigniter将函数(数据)警告为空

时间:2015-06-24 14:50:36

标签: php jquery ajax codeigniter

嗨我有这个消息传递系统。我在发送消息时使用ajax。消息运行良好,也可以保存在数据库中。当我刷新整个页面时,消息就在那里。我的问题是我希望当我输入消息并单击回复时,消息将预先添加到相应的html <div>,现在当我alert(data)时它似乎是空的。这是我下面的ajax代码

$.ajax({
              type: "get",
              url: "<?php echo base_url().'admin/messages/reply'?>",
              data: {
                replyMessage:replyMessage,
                adminId:adminId,
                getUserId:getUserId,
                msgId:msgId
              },
              success: function(data) {
                alert(data);
                $("#success").show();
                $("#replyMessage").val('');
                $("#success").fadeOut(3000);
                $(data).prependTo('conversation-item item-right clearfix').show();
              }
            });

我的回复功能

 public function reply(){
      $uri = $this->uri->segment(5);

            $msgId = $this->input->post('msgId');

      $adminId = $this->input->post('adminId');
      $replyMessage = $this->input->post('replyMessage');
      $getUserId = $this->input->post('getUserId');

      $dateCreated = date('Y-m-d H:i:s');

      $array = array(
              'userId'=>$adminId,
              'message'=>$replyMessage,
              'senderId'=>$getUserId,
                            'msgId'=>$msgId,
              'dateCreated'=>$dateCreated
              );
      $this->mm->sendMessage($array);
      $id = $this->db->insert_id(); 

    }

和我的观点页面

<?php foreach($getThreadMessage as $thread): ?>
                    <div class="conversation-item item-right clearfix">
                        <div class="conversation-user">
                          <img src="<?php echo base_url();?>assets/images/small_empty_image.png" alt=""/>
                        </div>
                        <div class="conversation-body">
                          <div class="name">
                            <?php echo ucwords($thread->firstName); ?> <?php echo ucwords($thread->lastName); ?>
                          </div>
                          <div class="time hidden-xs">
                            <?php echo date('M d, Y', strtotime($thread->dateUpdated)); ?>
                            <?php echo date('H:i a', strtotime($thread->dateUpdated)); ?>

                          </div>
                          <div class="text">
                            <?php echo $thread->message;?>
                          </div>
                        </div>
                    </div>
                    <?php endforeach; ?>

有人可以帮我解决这个问题吗?非常感谢任何帮助.TIA

1 个答案:

答案 0 :(得分:0)

在代码$id = $this->db->insert_id();

之后的控制器中

添加此代码

$data['getThreadMessage'] = $this->mm->get_message($id);
        print($this->load->view("getThreadMessage-view", $data));

您已在模型get_message($id);中创建了一个功能mm,并创建了一个名为getThreadMessage-view的视图

getThreadMessage视

<div class="conversation-item item-right clearfix">
                        <div class="conversation-user">
                          <img src="<?php echo base_url();?>assets/images/small_empty_image.png" alt=""/>
                        </div>
                        <div class="conversation-body">
                          <div class="name">
                            <?php echo ucwords($thread->firstName); ?> <?php echo ucwords($thread->lastName); ?>
                          </div>
                          <div class="time hidden-xs">
                            <?php echo date('M d, Y', strtotime($thread->dateUpdated)); ?>
                            <?php echo date('H:i a', strtotime($thread->dateUpdated)); ?>

                          </div>
                          <div class="text">
                            <?php echo $thread->message;?>
                          </div>
                        </div>
                    </div>

在jquery成功函数中

 $(".conversation-item.item-right.clearfix").prepend(data);