AJAX / PHP聊天刷新

时间:2017-06-01 06:46:37

标签: php ajax chat

使用AJAX进行此聊天功能,但需要刷新它以检索新的聊天。我尝试使用setIntervala和setTimeout。对不起,诺布这个。

message.php

   $(document).ready(function(){
    $("#btn-submit").click(function(e){
        e.preventDefault()
        var msgg = $("#chat-msg").val();
        var href = $('#btn-submit').attr('href');
        var ssid = $("#s-id").val();
        var rrid = $("#r-id").val();

        $.ajax({
          method: "POST",
          url: "http://127.0.0.1/test/admin/dashboard/ajaxMessage/",
          data: { 
                message: msgg
            }
        })

      .done(function(msg) {
        console.log(msg);

        $("#chat-msg").val("");

        var head; 
        if (rrid != ssid) {
            head = '<br><br><br><h1 class="msg-show-right">'; 
        } else {
            head = '<br><br><br><h1 class="msg-show-left">';
        }

        var chatBubble = head + " " + msgg + "</h1>";

        $( ".msg-show-right" ).each(function( index ) {
                last_bubble = $(this);

        });

        last_bubble.after(chatBubble)

      });
    });
});

dashboard.php

public function ajaxMessage () {

    $message = $this->input->post('message');
    $s_id = $this->session->userdata('s_id');
    $r_id = $this->session->userdata('r_id');


    $data = array(
        'msg' => $message,
        'status' => '1',
        's_id' => $s_id,
        'r_id' => $r_id
    );

    $this->admin_model->form_insert($data);
    echo "true";
}

还有一些不必要的代码。

0 个答案:

没有答案