04-21-2011, 03:15 AM
[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 :
When I call the function from my browser, it displays what is expected :
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 :
So please, what do I get wrong :question:
Thanks for reading !
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', '<?php echo base_url(); ?>languagesetup/setlanguage/english', true);
xmlhttp.send(null);
return true;
}
function switchToSwedish()
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', '<?php echo base_url(); ?>languagesetup/setlanguage/swedish', true);
xmlhttp.send(null);
return true;
}
So please, what do I get wrong :question:
Thanks for reading !