Welcome Guest, Not a member yet? Register   Sign In
[Solved] Modifying sessions with Javascript
#1

[eluser]Jilimay[/eluser]
Hello everyone,

I am posting here because I have a piece of code that won't work, and I really
would like to know why !

My original idea was to allow users to change my site's language by clicking on
corresponding flags, somewhere in the page. In order to do that, I thought I
would use the XMLHttpRequest object in Javascript, and call a given function
from a controller via a GET request on the good URL.

So here is how it looks (please note that the 'echo' and other 'var_dump' are
obviously there for testing purposes) :

First, the controller supposed to change the site's language :

Code:
class LanguageSetup extends CI_controller {

    public function setLanguage($newLanguage = 'english')
    {
        echo "<p>Switching language to $newLanguage</p>";
        $ref =& get_instance();
        $ref->session->set_userdata('language', $newLanguage);
        $ref->lang->load
                         (
                             $ref->session->userdata('language'),
                             $ref->session->userdata('language')
                          );
        var_dump($ref->session->userdata);
        echo '<p>'.$this->lang->line('hello').'</p>';
    }
}

When I call the function from my browser, it displays what is expected :

Code:
Switching language to english

array
  'session_id' => string 'f89dd59fdabbabc4c7c918146be57788' (length=32)
  'ip_address' => string '127.0.0.1' (length=9)
  'user_agent' => string 'Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/2010010' (length=50)
  'last_activity' => int 1303376217
  'language' => string 'english' (length=7)

Hello !

And of course if I open another tab, the settings are still the same.

But when I try to call it with a xmlhttprequest, nothing seems to happen. :roll:

Here is the Java'script :

Code:
function switchToEnglish()
{
    var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('GET', '&lt;?php echo base_url(); ?&gt;languagesetup/setlanguage/english', true);
    xmlhttp.send(null);
    return true;
}

function switchToSwedish()
{
    var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('GET', '&lt;?php echo base_url(); ?&gt;languagesetup/setlanguage/swedish', true);
    xmlhttp.send(null);
    return true;
}


So please, what do I get wrong :question:

Thanks for reading !


Messages In This Thread
[Solved] Modifying sessions with Javascript - by El Forum - 04-21-2011, 03:15 AM
[Solved] Modifying sessions with Javascript - by El Forum - 04-24-2011, 10:45 AM
[Solved] Modifying sessions with Javascript - by El Forum - 04-25-2011, 09:48 PM
[Solved] Modifying sessions with Javascript - by El Forum - 04-26-2011, 12:21 AM
[Solved] Modifying sessions with Javascript - by El Forum - 04-26-2011, 01:08 AM
[Solved] Modifying sessions with Javascript - by El Forum - 04-26-2011, 01:18 AM
[Solved] Modifying sessions with Javascript - by El Forum - 04-26-2011, 01:56 AM
[Solved] Modifying sessions with Javascript - by El Forum - 04-26-2011, 06:23 PM
[Solved] Modifying sessions with Javascript - by El Forum - 04-27-2011, 12:42 AM
[Solved] Modifying sessions with Javascript - by El Forum - 04-27-2011, 01:36 AM
[Solved] Modifying sessions with Javascript - by El Forum - 04-27-2011, 02:46 AM
[Solved] Modifying sessions with Javascript - by El Forum - 04-27-2011, 03:35 AM
[Solved] Modifying sessions with Javascript - by El Forum - 07-16-2011, 02:40 PM



Theme © iAndrew 2016 - Forum software by © MyBB