The Session does not work in my code. It works when page is refreshed. I have a controller named ravi
and the view name is myview
. The code is:
<?php
class Ravi extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('html');
$this->load->database();
$this->load->library('form_validation');
$this->load->library('encrypt');
$this->load->helper(array('form', 'url'));
//load the login model
// $this->load->model('login_model');
}
function index() {
$this->session->set_flashdata("f1", "<b>Welcome..!!</b>");
$this->load->view('myview.php');
}
function get_registration() {
$this->session->set_flashdata("f2", "<b>Welcome You have Successsfully Registered..!!</b>");
}
function get_login() {
$this->session->set_flashdata("f3", "<b>Welcome You have Successsfully Logged In..!!</b>");
}
function view() {
$this->session->set_flashdata("f4", "<b>Welcome You Can view..!!</b>");
}
function Logout() {
$this->session->set_flashdata("ravi", "<b>Welcome You have Successsfully Registered Logged Out..!!</b>");
}
}
?>
and my view is:
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
function Registration() {
$.ajax({
url: "<?php echo base_url() . 'index.php/ravi/get_registration' ?>",
type: 'POST', //the way you want to send data to your URL
success: function (data) {
if (data) {
$("div#view").html(data);
}
}
});
}
function View() {
$.ajax({
url: "<?php echo base_url() . 'index.php/ravi/view' ?>",
type: 'POST', //the way you want to send data to your URL
success: function (data) {
if (data) {
$("div#view").html(data);
}
}
});
}
function Login() {
$.ajax({
url: "<?php echo base_url() . 'index.php/ravi/get_login' ?>",
type: 'POST', //the way you want to send data to your URL
success: function (data) {
if (data) {
$("div#view").html(data);
}
}
});
}
function Logout() {
$.ajax({
url: "<?php echo base_url() . 'index.php/ravi/Logout' ?>",
type: 'POST', //the way you want to send data to your URL
success: function (data) {
if (data) {
$("div#view").html(data);
}
}
});
}
</script>
</head>
<body style="background-color: #99BC99">
<div id="mydiv" align="center">
<a href="javascript:ravi();"><b>Home</b></a>
<a href="javascript:Registration();"><b>Registration</b></a>
<a href="javascript:View();"><b>View Users</b></a>
<a href="javascript:Login();"><b>Login</b></a>
<a href="javascript:Logout();"><b>Logout</b></a>
</div>
<div id="view">
<?php
if ($this->session->flashdata('ravi')) {
echo $this->session->flashdata('ravi');
}
if ($this->session->flashdata('f1')) {
echo $this->session->flashdata('f1');
}
if ($this->session->flashdata('f2')) {
echo $this->session->flashdata('f2');
}
if ($this->session->flashdata('f3')) {
echo $this->session->flashdata('f3');
}
if ($this->session->flashdata('f4')) {
echo $this->session->flashdata('f4');
}
?>
</div>
</body>
</html>
Session can be realized by the view only after the session is set. You don't need to refresh the page to make the session set.
when i call ajax means click on the link session messege must be displayed but it does not. it works after refresh
Your server knows that the session is set, but your old rendered html doesn't knows it.
Tiny Hack :
If you want to do it without refresh then you shall refresh the div content after the session is set
i.e.,
You should replace the content inside the div with the fresh value. In the ajax success you should display the value that is assigned to the session.
Note :
I hope you are trying to do it the same what i explain but you are missing here
success: function (data) {
if (data)
{
$("div#view").html(data);
}
}
You shall replace the old value by new only by having a separate div with unique id.
Something like
$("#YourExclusiveDiv").html(data);