javascript - I cant get user details to save properly in a session -
i trying create chat box , cant user_id save in session after logging in , saving in database.
this login function
public function login() { $this->form_validation->set_rules('email', 'username', 'trim|required|xss_clean'); $this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean'); if ($this->form_validation->run() == false) { // return main page if submitted form invalid. $this->load->view('abt_login'); } else { $this->load->model('abt_db'); $q = $this->abt_db->check_login( $this->input->post('email'), $this->input->post('password') ); if ($q) { redirect('index.php/abovetheblues/abt_abovetheblues'); $this->abt->set_session(); } else { $this->show_login(true); } } }
this javascript code
$(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; });
this controller
function add_chat_messages() { // grab $chat_message_content, $user_id $user_id = $this->input->post($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); }
this model
function check_login($email, $password) { $this->load->database(); // query retrieve user's details // based on received username , password $this->db->from('user'); $this->db->where('email', $email); $this->db->where('password', $password); $q = $this->db->get()->result(); // results of query stored in $q. // if value exists, user account exists , validated if (is_array($q) && count($q) == 1) { // set users details $details property of class $this->details = $q[0]; // call set_session set user's session $this->set_session(); return true; } return false; } function set_session() { // session->set_userdata codeigniter function // stores data in cookie in user's browser. of values built in // codeigniter, others added (like user_id). $this->session->set_userdata(array( 'user_id' => $this->details->user_id, 'email' => $this->details->email, 'username' => $this->details->username, 'isloggedin' => true ) ); } function add_chat_message($user_id, $chat_message_content) { $query_str = "insert chat_message(user_id, chat_message_content) values (?,?,?)"; $this->db->query($query_str, array($user_id, $chat_message_content)); }
and view page
<script type="text/javascript"> var user_id = "<?php echo $this->session->userdata("user_id"); ?>"; var base_url = "<?php echo base_url();?>"; </script> <!--loads header--> <?php $this->load->view('abt-header'); ?> <!--this 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>
well if user_id defined in session no need send every time trough javascript, or define in page javascript variable.
in controller have give variable name passed trough javascript, bu calling session data give id not variable name
$user_id = $this->input->post($this->session->userdata("user_id")); // wrong $user_id = $this->input->post("user_id"); // right $user_id = $this->session->userdata("user_id"); // way without pass through javascript
Comments
Post a Comment