【问题标题】:Updating Table or div Data using Ajax Codeigniter使用 Ajax Codeigniter 更新表或 div 数据
【发布时间】:2018-04-13 07:04:23
【问题描述】:

我正在尝试创建一个页面,其中显示客户当前进行的聊天。 现在我使用 Openfire 作为服务器,使用 PHP Ajax 编写脚本。

Openfire 将数据转储到 MySQL 表中。 我使用 Codeigniter-PHP 检索所有记录:

 public function get_chats()
    {
        $this->db->select('*');
        $this->db->from('ofMessageArchive');
        $query = $this->db->get();
        $result = $query->result();


        $this->data['messages'] = $result;


        $this->data['subview'] = 'dashboard/test';
        $this->load->view('_layout_main', $this->data);
    }

现在在我看来我有桌子:

<table class="table table-striped table-bordered table-hover" >
         <thead>
             <th>From</th>
             <th>To</th>
             <th>Message</th>
             <th>Time</th>
         </thead>

         <tbody>
            <?php if(count($messages)): foreach($messages as $key => $message): ?> 
            <tr> 
                <td><?php  $users =  explode("__", $message->fromJID); echo $users[0];?></td>
                <td><?php $tousers =  explode("__", $message->toJID); echo $tousers[0];  ?></td>
                <td><i class="material-icons">play_circle_filled</i>Message</td>
                <td><?php echo $date->format('d-m-y H:i:s'); ?></td>
            </tr>
            <?php endforeach; ?>

            <?php endif; ?>
    </tbody>
    </table>           

现在我不想重新加载整个页面,而是每 5 秒刷新一次表格内容。

我正在使用 DataTables

我还以为我可以将 json 传递给我的视图,但我不知道如何每 5 秒更新一次。

public function get_chats()
    {
        $this->db->select('*');
        $this->db->from('ofMessageArchive');
        $query = $this->db->get();
        $result = $query->result();


        if (count($result)) {
            $response =  json_encode($result);
        }
        else
        {
            $response = false;
        }

        echo $response;
    }

这也将发送新的和旧的消息。 所以我只想将新闻消息附加到表格旧消息不应该重复

【问题讨论】:

  • 它称为长池

标签: php jquery ajax codeigniter datatable


【解决方案1】:

你的后端功能:

 public function get_chats()
    {
        $this->db->select('*');
        $this->db->from('ofMessageArchive');
        $query = $this->db->get();
        $result = $query->result();


        if (count($result)>0) {
            $response['status']= true;
            $response['messages']= $result;
            $response['date']= date("d-m-y H:i:s", strtotime($your-date-here));
        }
        else
        {
            $response['status']=false;
        }

        echo json_encode($response);
    }

你必须像这样制作javascript函数:

<script>

function myAJAXfunction(){
 $.ajax({
     url:'your url here',
     type:'GET',
     dataType:'json',
     success:function(response){
         console.log(response);

         var trHTML='';
         if(response.status){
           for(var i=0;i<response.messages.length;i++){

              users =response.messages[i]fromJID.slice('--');
              touser=response.messages[i]toJID.slice('--');

              trHTML=trHTML+
              '<tr>'+
               '<td>'+users[0]+'</td>'+
               '<td>'+touser[0]+'</td>'+
               '<td><i class="material-icons">play_circle_filled</i>Message</td>'+
               '<td>'+response.date+'</td>'+
               </tr>';
         }

           $('tbody').empty();
           $('tbody').append(trHTML);

         }else{
           trHTML='<tr><td colspan="4">NO DATA AVAILABLE</td></tr>';
           $('tbody').empty();
           $('tbody').append(trHTML);
         }

     },
     error:function(err){
        alert("ERROR LOADING DATA");
     }
 });
}


// calling above ajax function  at every 5 seconds

setInterval(myAJAXfunction,5000);

</script>

【讨论】:

  • 嘿,Saurabh,谢谢!我收到此错误 :: Uncaught SyntaxError: Unexpected identifier on this line ::: users =response.messages[i]fromJID.slice('__');
  • 我应该对我的 html 进行任何更改吗??
  • 尝试先打印这个:console.log(response.messages[i]fromJID)
  • 我解决了这个问题,但最后一个问题 :) 我收到消息:&lt;message xmlns:cli="jabber:client" to="jhon__umobilityptt.7600556046@ptt.fms.bizrtc" cli:subject="TEXT MESSAGE" cli:lang="en" type="chat" id="" from="rylie__umobilityptt.7600556046@ptt.fms.bizrtc/bizRTC"&gt;&lt;body&gt;OFFLINE_READ_RECEIPT&lt;/body&gt;&lt;cha:active xmlns:cha="http://jabber.org/protocol/chatstates"&gt;&lt;/cha:active&gt;&lt;/message&gt;
  • 您需要在回复中使用 Javascript 进行 XML 解析:希望此链接对您有所帮助:stackoverflow.com/questions/17604071/parse-xml-using-javascript
猜你喜欢
  • 2019-10-31
  • 1970-01-01
  • 2021-02-01
  • 2020-07-12
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
相关资源
最近更新 更多