簡體   English   中英

Codeigniter 聊天應用程序未提交表單數據並在 javascript 控制台中顯示 500 內部服務器錯誤

[英]Codeigniter Chat Application Not submitting form data and also giving displaying an 500 Internal Server Error in javascript console

我正在嘗試使用 codeigniter 框架實現一個聊天應用程序,但是在表單提交時,表單數據沒有被提交,並且提交按鈕被禁用。 當我檢查 javascript 控制台時,我收到錯誤 500 Internal Server Error for the url localhost/biko/chat/conv/xsapSHKs/msg 它應該獲取新消息。 我對 PHP 非常熟悉,但對 ajax 或 javascript 不太好。 任何幫助將不勝感激。 下面是我的控制器、模型和視圖。

控制器(chat.php)

public function conv($slug = FALSE, $action = FALSE) {
    $this->form_validation->set_rules('message', 'Message', 'trim|required');
    $this->form_validation->set_rules('mid', '', 'trim|required');
    $this->form_validation->set_rules('fid', '', 'trim|required');
    $this->form_validation->set_rules('id', '', 'trim|required');
    if ($slug === FALSE) {
        if ($_POST['bcdhgfjhggvgzs'] != '') {
            $this->Chat_model->startConv();
            redirect('/conv/chat/'.$_POST['bcdhgfjhggvgzs'], 'refresh');
        }
    } else {
        if ($action !== '' and $this->form_validation->run() !== FALSE) {
            $this->Chat_model->save($action);
        }
        $data['pageTitle'] = 'Chat';                
        $data['pageKeywords'] = 'chat';
        $data['pageDescription'] = 'Chat with Us';
        $data['converse'] = $this->Chat_model->get_conversations($slug);
        $data['ifempty'] = count($data['converse']);
        $data['content'] = "chat/chatty";
        $this->load->view($this->frontend_template, $data);
    }
}

模型

public function save($action) {
    $from = $this->input->post('mid', TRUE);
    $descId = $this->input->post('id', TRUE);
    $to = $this->input->post('fid', TRUE);
    $msg = $this->input->post('text', TRUE);
    $created = mktime();
    
    switch ($action):
    case 'new':
        $descId = $this->input->post('id', TRUE);
        $created = mktime();
        $msg = $this->input->post('text', TRUE);
        $from = $this->input->post('mid', TRUE);
        $to = $this->input->post('fid', TRUE);
        if(empty($msg)){
            $json = array('status' => 0, 'msg'=> 'Enter your message!.');
        }else{
            $data = array (
                'desc_id' => $descId,
                'from' => $from,
                'to' => $to,
                'msg' => $msg,
                'time' => $created,
                'status' => '1'
            );      
            $qur = $this->db->insert('chat_msg', $data);
            $insertId = $this->db->insert_id();
            if($qur){
                $this->db->where('id', $insertId);
                $qurGet = $this->db->get('chat_msg');
                while($row = $qurGet->row_array()){
                    $json = array('status' => 1, 'msg' => $row['msg'], 'lid' => $insertId, 'time' => $row['time']);
                }
            } else {
                $json = array('status' => 0, 'msg'=> 'Unable to process request.');
            }
        }
    break;
    case 'msg':
        $from = $this->input->post('mid', TRUE);
        $to = $this->input->post('fid', TRUE);
        $lid = $this->input->post('lid', TRUE);
        if(empty($from)){

        } else {
            //print_r($_POST);
            $where = array('to' => $to, 'from' => $from, 'status' => '1');
            $this->db->where($where);
            $qur = $this->db->get('chat_msg');
            if($qur->num_rows() > 0){
                $json = array('status' => 1);
            }else{
                $json = array('status' => 0);
            }
        }
    break;
    case 'NewMsg':
        $from = $this->input->post('mid', TRUE);
        $to = $this->input->post('fid', TRUE);
        
        $where = array('to' => $to, 'from' => $from, 'status' => '1');
        $this->db->where($where);
        $this->db->order_by('id', 'desc');
        $this->db->limit(1);
        $qur = $this->db->get('chat_msg');

        while($rw = $qur->row_array()){
            $json = array('status' => 1, 'msg' => '<div>'.$rw['msg'].'</div>', 'lid' => $rw['id'], 'time'=> $rw['time']);
        }
        // update status
        $where = array('to' => $to, 'from' => $from);
        $this->db->where($where);
        $qur = $this->db->update('chat_msg');
    break;
endswitch;
$this->output->set_content_type('application/json');
return $this->set_output($json);
}

看法

    <?php
if ($this->auth_user_id != $converse['from']) { $fid = $this->auth_user_id; } else { $fid = $converse['to']; }
$myid = $this->auth_user_id;
?>
<script src="<?php echo WEBSITE_URL ?>assets/chat/jquery.min.js"></script>
<script src="<?php echo WEBSITE_URL ?>assets/chat/moment.min.js"></script>
<script src="<?php echo WEBSITE_URL ?>assets/chat/livestamp.js"></script>
<link rel="stylesheet" type="text/css" href="<?php echo WEBSITE_URL ?>assets/chat/style.css">
<div class="chatcontainer">
      <div class="chat" id="chat">
        <div class="stream" id="cstream">

      </div>
      </div>
      <div class="msg">
          <form method="post" id="msenger" action="">
            <textarea name="msg" id="msg-min"></textarea>
            <input name="id" type="hidden" value="<?php echo $converse['id'] ?>" />
            <input type="hidden" name="mid" value="<?php echo $myid;?>">
            <input type="hidden" name="fid" value="<?php echo $fid;?>">
            <input type="submit" value="Send" id="sb-mt">
          </form>
      </div>
      <div id="dataHelper" last-id=""></div>
  </div>
<script type="text/javascript">
$(document).keyup(function(e){
    if(e.keyCode == 13){
        if($('#msenger textarea').val().trim() == ""){
            $('#msenger textarea').val('');
        }else{
            $('#msenger textarea').attr('readonly', 'readonly');
            $('#sb-mt').attr('disabled', 'disabled');   // Disable submit button
            sendMsg();
        }       
    }
}); 

$(document).ready(function() {
    $('#msg-min').focus();
    $('#msenger').submit(function(e){
        $('#msenger textarea').attr('readonly', 'readonly');
        $('#sb-mt').attr('disabled', 'disabled');   // Disable submit button
        sendMsg();
        e.preventDefault(); 
    });
});

function sendMsg(){
    $.ajax({
        type: 'post',
        url: '<?php echo WEBSITE_URL .''. uri_string() ?>/new',
        data: $('#msenger').serialize(),
        dataType: 'json',
        success: function(rsp){
                $('#msenger textarea').removeAttr('readonly');
                $('#sb-mt').removeAttr('disabled'); // Enable submit button
                if(parseInt(rsp.status) == 0){
                    alert(rsp.msg);
                }else if(parseInt(rsp.status) == 1){
                    $('#msenger textarea').val('');
                    $('#msenger textarea').focus();
                    //$design = '<div>'+rsp.msg+'<span class="time-'+rsp.lid+'"></span></div>';
                    $design = '<div class="float-fix">'+
                                    '<div class="m-rply">'+
                                        '<div class="msg-bg">'+
                                            '<div class="">'+
                                            'Me'+
                                            '</div>'+
                                            '<div class="msgA">'+
                                                rsp.msg+
                                                '<div class="">'+
                                                    '<div class="msg-time time-'+rsp.lid+'"></div>'+
                                                    '<div class="myrply-i"></div>'+
                                                '</div>'+
                                            '</div>'+
                                        '</div>'+
                                    '</div>'+
                                '</div>';
                    $('#cstream').append($design);

                    $('.time-'+rsp.lid).livestamp();
                    $('#dataHelper').attr('last-id', rsp.lid);
                    $('#chat').scrollTop($('#cstream').height());
                }
            }
        });
}
function checkStatus(){
    $fid = '<?php echo $fid; ?>';
    $mid = '<?php echo $myid; ?>';
    $.ajax({
        type: 'post',
        url: '<?php echo WEBSITE_URL .''. uri_string() ?>/msg',
        data: {fid: $fid, mid: $mid, lid: $('#dataHelper').attr('last-id')},
        dataType: 'json',
        cache: false,
        success: function(rsp){
                if(parseInt(rsp.status) == 0){
                    return false;
                }else if(parseInt(rsp.status) == 1){
                    getMsg();
                }
            }
        }); 
}

// Check for latest message
setInterval(function(){checkStatus();}, 200);

function getMsg(){
    $fid = '<?php echo $fid; ?>';
    $mid = '<?php echo $myid; ?>';
    $.ajax({
        type: 'post',
        url: '<?php echo WEBSITE_URL .''. uri_string() ?>/NewMsg',
        data:  {fid: $fid, mid: $mid},
        dataType: 'json',
        success: function(rsp){
                if(parseInt(rsp.status) == 0){
                    //alert(rsp.msg);
                }else if(parseInt(rsp.status) == 1){
                    $design = '<div class="float-fix">'+
                                    '<div class="f-rply">'+
                                        '<div class="msg-bg">'+
                                            '<div class="">'+
                                            $fid+
                                            '</div>'+
                                            '<div class="msgA">'+
                                                rsp.msg+
                                                '<div class="">'+
                                                    '<div class="msg-time time-'+rsp.lid+'"></div>'+
                                                    '<div class="myrply-f"></div>'+
                                                '</div>'+
                                            '</div>'+
                                        '</div>'+
                                    '</div>'+
                                '</div>';
                    $('#cstream').append($design);
                    $('#chat').scrollTop ($('#cstream').height());
                    $('.time-'+rsp.lid).livestamp();
                    $('#dataHelper').attr('last-id', rsp.lid);  
                }
            }
    });
}
</script>

Javascript 控制台截圖javascript 控制台的屏幕截圖

嘗試更改$config['permitted_uri_chars'] = ''; 在config.php文件中。

請嘗試,讓我知道。

而不是檢查控制台錯誤檢查請求的網絡選項卡響應

  1. 檢查您之前加載的form_validation庫和模型
  2. PHP Lint控制器和模型文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM