JavaScript codeigniter session doesn't update on ajax request codeigniter session doesn't work

why is it that codeigniter session doesn't update values when an ajax request is done?

controller index:

public function index()
{
    $this->session->set_userdata( 'greetings', 'hello!' );
}

ajax request:

$.ajax({
    type: "POST",
    url: "<?php echo site_url("set_greeting"); ?>",
    data: data,
    cache: false,
    success: function(response){
        alert('<?= $this->session->userdata('greetings'); ?>');
    }
});

controller:

public function set_greeting()
    {
        $this->session->set_userdata( 'greetings', 'hi!' );
        echo $this->session->userdata('greetings');
    }

after the ajax request is made, I've checked firebug's console and the controller's response echoed "hi!" but the alerted value from the success of ajax is still "hello".

Answer:1

PHP is running on server side and all PHP code is rendered before sending anything to the client so alert('<?= $this->session->userdata('greetings'); ?>'); is rendered to alert('hello');.

When you do

success: function(response){ alert('<?= $this->session->userdata('greetings'); ?>'); }

it is basically saying to alert hello if the ajax request succeeds.

since PHP cannot reflect any changes done on the server side without a page refresh you cannot see changes done to server session but if you refresh the page you will be able to see the changed value because '<?= $this->session->userdata('greetings'); ?>' is renderd again on the server and now its value is changed to hi.(But in your code you won't be able to see the session change since your index method always set the value to hello. Changed value will always overwritten with the page refresh but if you use any other controller method in your system which does not set that particular session value you can reflect the change.)

Why this happens in your code ?

This happens because you're not using the response to your ajax request. You're correctly changing the server session and response is also correct but since you're not using the response you cannot see the modified value.Use the response and you should be all good.

Answer:2
alert('<?= $this->session->userdata('greetings'); ?>');

This code will render before your ajax request..

alert(response);

use this to see your updated session value;

Answer:3

I want to update a variable called hideProgressBar the Directive "ng-hide" in this view through the $ scope in my control. But it does not work. The line below works: $ Scope.hideProgessBar = true; ...

I want to update a variable called hideProgressBar the Directive "ng-hide" in this view through the $ scope in my control. But it does not work. The line below works: $ Scope.hideProgessBar = true; ...

I need to pas params to Aurelia on start. Depending on values passed the application will have diffrent state. This application is injected on page which is build with PHP, so the best way would be ...

I need to pas params to Aurelia on start. Depending on values passed the application will have diffrent state. This application is injected on page which is build with PHP, so the best way would be ...

I want to achieve the following hover effect on my portfolio elements - link with before and after. I am using Bootstrap with Masonry so the HTML for the original image is looking like that: <div ...

I want to achieve the following hover effect on my portfolio elements - link with before and after. I am using Bootstrap with Masonry so the HTML for the original image is looking like that: <div ...

  1. what is hover effect
  2. what is hover effect in css
  3. what is hover effect in html

I use AudioContext to play some audios in my site. It works fine on Chrome and Firefox, but not on Safari. On Safari stop function does not work and I get the following: [Error] InvalidStateError: ...

I use AudioContext to play some audios in my site. It works fine on Chrome and Firefox, but not on Safari. On Safari stop function does not work and I get the following: [Error] InvalidStateError: ...