codeigniter聊天框不起作用

时间:2015-10-01 11:16:57

标签: php codeigniter chat

我在codeigniter中创建一个聊天框,但在输入名称和消息后,弹出框将显示“禁止”。

我真的很困惑我把它放在shout.php这里(chatbox.php')  $ .post('shout.php',load_data,function(data){

而不是'shout.php'我放了SelectMany

我的控制器 money_c

List<User>
{
    { UserID = 576, PostId = 7 },
    { UserID = 576, PostId = 4 },
    { UserID = 576, PostId = 9 },
    { UserID = 2, PostId = 5 },
    { UserID = 2, PostId = 1 },
    { UserID = 4, PostId = 2 }
}

chatbox.php

function chat(){

    $this->load->view('chatbox');
}

shout.php

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Chat Box</title>
<style type="text/css">
<!--
.shout_box {
    background: #627BAE;
    width: 260px;
    overflow: hidden;
    position: fixed;
    bottom: 0;
    right: 20%;
    z-index:9;
}
.shout_box .header .close_btn {
    background: url(images/close_btn.png) no-repeat 0px 0px;
    float: right;
    width: 15px;
    height: 15px;
}
.shout_box .header .close_btn:hover {
    background: url(images/close_btn.png) no-repeat 0px -16px;
}

.shout_box .header .open_btn {
    background: url(images/close_btn.png) no-repeat 0px -32px;
    float: right;
    width: 15px;
    height: 15px;
}
.shout_box .header .open_btn:hover {
    background: url(images/close_btn.png) no-repeat 0px -48px;
}
.shout_box .header{
    padding: 5px 3px 5px 5px;
    font: 11px 'lucida grande', tahoma, verdana, arial, sans-serif;
    font-weight: bold;
    color:#fff;
    border: 1px solid rgba(0, 39, 121, .76);
    border-bottom:none;
    cursor: pointer;
}
.shout_box .header:hover{
    background-color: #627BAE;
}
.shout_box .message_box {
    background: #FFFFFF;
    height: 200px;
    overflow:auto;
    border: 1px solid #CCC;
}
.shout_msg{
    margin-bottom: 10px;
    display: block;
    border-bottom: 1px solid #F3F3F3;
    padding: 0px 5px 5px 5px;
    font: 11px 'lucida grande', tahoma, verdana, arial, sans-serif;
    color:#7C7C7C;
}
.message_box:last-child {
    border-bottom:none;
}
time{
    font: 11px 'lucida grande', tahoma, verdana, arial, sans-serif;
    font-weight: normal;
    float:right;
    color: #D5D5D5;
}
.shout_msg .username{
    margin-bottom: 10px;
    margin-top: 10px;
}
.user_info input {
    width: 98%;
    height: 25px;
    border: 1px solid #CCC;
    border-top: none;
    padding: 3px 0px 0px 3px;
    font: 11px 'lucida grande', tahoma, verdana, arial, sans-serif;
}
.shout_msg .username{
    font-weight: bold;
    display: block;
}
-->
</style>

<script type="text/javascript" src="<?php echo base_url();?>assets/js/jquery-1.9.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

    // load messages every 1000 milliseconds from server.
    load_data = {'fetch':1};
    window.setInterval(function(){
     $.post('shout.php', load_data,  function(data) {
        $('.message_box').html(data);
        var scrolltoh = $('.message_box')[0].scrollHeight;
        $('.message_box').scrollTop(scrolltoh);
     });
    }, 1000);

    //method to trigger when user hits enter key
    $("#shout_message").keypress(function(evt) {
        if(evt.which == 13) {
                var iusername = $('#shout_username').val();
                var imessage = $('#shout_message').val();
                post_data = {'username':iusername, 'message':imessage};

                //send data to "shout.php" using jQuery $.post()
                $.post('shout.php', post_data, function(data) {

                    //append data into messagebox with jQuery fade effect!
                    $(data).hide().appendTo('.message_box').fadeIn();

                    //keep scrolled to bottom of chat!
                    var scrolltoh = $('.message_box')[0].scrollHeight;
                    $('.message_box').scrollTop(scrolltoh);

                    //reset value of message box
                    $('#shout_message').val('');

                }).fail(function(err) { 

                //alert HTTP server error
                alert(err.statusText); 
                });
            }
    });

    //toggle hide/show shout box
    $(".close_btn").click(function (e) {
        //get CSS display state of .toggle_chat element
        var toggleState = $('.toggle_chat').css('display');

        //toggle show/hide chat box
        $('.toggle_chat').slideToggle();

        //use toggleState var to change close/open icon image
        if(toggleState == 'block')
        {
            $(".header div").attr('class', 'open_btn');
        }else{
            $(".header div").attr('class', 'close_btn');
        }


    });
});

</script>
</head>

<body>
<div class="shout_box">
<div class="header">chat box<div class="close_btn">&nbsp;</div></div>
  <div class="toggle_chat">
  <div class="message_box">
    </div>
    <div class="user_info">
    <input name="shout_username" id="shout_username" type="text" placeholder="Your Name" maxlength="15" />
   <input name="shout_message" id="shout_message" type="text" placeholder="Type Message Hit Enter" maxlength="100" /> 
    </div>
    </div>
</div>
</body>
</html>

但输入名称和消息警告框后会弹出显示'禁止'。

1 个答案:

答案 0 :(得分:0)

我不认为你完全掌握了MVC架构,这不是解释它的地方。

我建议你更多地研究它,但是这里可能对你有用的是修改你的控制器功能:

function chat(){
    $this->load->view('chatbox');    
}

function shout(){
    $this->load->view('shout');
}

然后,您需要确保URL(路由)有效。

假设您当前的网址是www.mysite.com/someController/chat /

然后新的URL将是www.mysite.com/someController/shout /

如果此网址不起作用,那么您需要整理路线以使其有效。

如果此URL有效,则需要从

更新JQuery URL
.post('shout.php', load_data,  function(data) {

.post('/someController/shout/', load_data,  function(data) {

TL; DR jquery post函数通过URL访问fule,就像真人一样。它无法直接加载文件。