Reputation: 1181
I was wondering if there is a way to set session variable in Ajax response, which would basically look like this:
// this is response
function(response)
{
<?php $_SESSION['ID'] = response.id ?>
// or something similar to this?
}
And then afterwards when I need to use the session so that I can access it.
// (Some php file)
$var_id = $_SESSION['ID'];
Is this doable?
Upvotes: 0
Views: 405
Reputation: 435
You can use HTML5 sessionStorage Object to save the data locally but for a single session . The data is deleted when the user closes the specific browser tab.
sessionStorage.id = response.id ;
Upvotes: 0
Reputation: 1187
Here are three functions to set, get, and delete cookies
https://jsfiddle.net/stevenkaspar/gnoj9aop/
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length,c.length);
}
}
return "";
}
function deleteCookie(cname){
document.cookie = cname+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
Upvotes: 1