我已设置会话,我希望使用ajax和jquery将数据登录autho控制器发送到配置文件控制器
public function login_autho() {
$this->load->library('session');
$data = array(
'uname' => $this->input->post('uname'),
'upassword' => $this->input->post('upassword')
);
//fetch database usename and password
$result = $this->login_model->login_user($data);
//user name put in the session
$data_session_set = array('logged' => 1, 'uname' => $this->input->post('uname'));
$data = $this->session->set_userdata($data_session_set);
print_r($result);
}
public function profile() {
$this->load->view('header');
//How to send user name from here to success view page and fetch all data
$this->load->view('user/success');
}
这是我登录表单中的ajax脚本,用于检查用户名并传递然后登录
<script>
$('#login_form').submit(function (e)
{
e.preventDefault();
var uname = $('#uname').val();
var upassword = $('#upassword').val();
if (uname == "" || upassword == "")
{
$('#error').show().html('All Fields are required');
} else {
$('#error').html("").hide();
$.ajax({
type: "POST",
url: "<?= base_url(); ?>User_controller/login_autho/",
data: {uname: uname, upassword: upassword},
success: function (data) {
//if session is set then redirect to User_controller/profile but session can send data
if (data == '1')
{
window.location.replace("<?= base_url(); ?>User_controller/profile");
} else
{
alert('invalid');
}
}
});
return false;
}
});
</script>
答案 0 :(得分:0)
您可以通过编写
简单地在codeigniter中获取会话数据$username = $this->session->userdata('uname');//uname is the session variable name.
您可以像这样从ajax向其他控制器发送数据。
<input hidden name="uname" value="<?php echo $username ?>" id="uname">
function(){
var uname = $("#uname").val();
$.ajax({
url: "" ,
type : "POST",
data:{username:uname},
}).done(function(data){
}).fail(function(data){
console.log("hello fail");
});//end fail
}
您也可以像这样发送其他数据字段。希望这会帮助你。
答案 1 :(得分:0)
在控制器
中更改此内容$data_session_set = array(
'uname' => $this->input->post('uname'),
'logged_in' => TRUE # add this insted of 'logged' => 1
);
$this->session->set_userdata($data_session_set);
我建议您使用
logged_in
代替logged
在您的视图中添加
echo $this->session->userdata('uname');
或尝试
print_r($this->session->userdata('uname'));
参考文献