0

I am creating a chat box, retrieving the user_id saved in the session and then using the ajax post but for some reason the user_id that is saved in the session is empty and not saving in the database and I am stocked now.

Controller

function add_chat_messages() {
        // Grab the $chat_message_content, $user_id and $chat_id 
        $user_id = $this->session->userdata("user_id");
        $chat_message_content = $this->input->post('chat_message_content');

        $this->abt_db->add_chat_message($user_id, $chat_message_content);
    }

Model

function add_chat_message($user_id, $chat_message_content) {

    $query_str = "INSERT INTO chat_message(user_id, chat_message_content) VALUES (?,?)";
    $this->db->query($query_str, array($user_id, $chat_message_content));
}

function set_session() {
        // session->set_userdata is a CodeIgniter function that
        // stores data in a cookie in the user's browser.  Some of the values are built in
        // to CodeIgniter, others are added (like the user_id).  
        $this->session->set_userdata(array(
            'user_id' => $this->details->user_id,
            'email' => $this->details->email,
            'username' => $this->details->username,
            'isLoggedIn' => true
                )
        );

    }

View

<script type="text/javascript">

    var user_id = "<?php echo $this->session->userdata('user_id'); ?>";
    var base_url = "<?php echo base_url(); ?>";


</script>

<!--loads the header-->
<?php $this->load->view('abt-header'); ?>
<!--this is the login page-->

<div data-role="page" id="Abt-chat" data-add-back-btn="true">
    <div data-role="header" data-position="fixed">
        <h1>Peer Chat</h1>

    </div>
    <div data-role="content">

        <div data-role="fieldcontain">
            <div id="chat_viewport"></div>

            <p>
                <label>Input Chat: </label>
                <input name="chat" id="chat" type="text" value=""/>

            </p>

            <p>
                <?php echo anchor('#', 'Send Chat', array('title' => 'Send Chat', 'id' => 'submit')); ?>
            </p>  

        </div> 
        <?php echo form_close(); ?>

    </div>

</div>

Javascript file

$(document).bind("mobileinit", function(){
    $.mobile.ajaxEnabled = false;
}); 


$(document).ready(function(){

    $("a#submit").click(function(){

        var chat_message_content = $("input#chat").val();

        if(chat_message_content == ""){

            return false;
        }

        $.post(base_url + "index.php/abovetheblues/add_chat_messages", {
            chat_message_content : chat_message_content,
            user_id : user_id
        }, 
        function(data){

            alert(data);
        },"json");


        return false;
    });
    return false;

});
4

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.