I need to change a session value inside a JavaScript function so I decided to use AJAX. However, the call does not seem to be working. What is wrong?
Index page setting default session value:
session_start();
if( is_null($_SESSION['lang']) ){
$_SESSION['lang'] = 'english';
}
AJAX call on index page:
var request = new XMLHttpRequest();
request.open('GET', 'setlang.php?entry=0', false);
request.send();
The setlang.php file:
$entry = $_GET['entry'];
if($entry == 0){
$_SESSION['lang'] = 'english';
Print_r ($_SESSION);
}else if($entry == 1){
$_SESSION['lang'] = 'japanese';
Print_r ($_SESSION);
}
Nothing is being printed. Does anyone know what is wrong?
Add this on the top of setlang.php:
session_start();
You didn't actually have anyway to check the response (unless you omitted some part of your javascript).
Check MDN's XMLHttpRequest's documentation.
var request = new XMLHttpRequest();
request.onload = function() {
alert(this.responseText);
}
request.onerror = function(e) {
alert('Error : ' + e.target.status);
}
request.open('GET', 'setlang.php?entry=0', false);
request.send();
Also make sure you call session_start();
in your php file.