I am attempting to store a javascript variable in a cookie so I can call it with PHP. I've never done this before, so pardon my naivety. I think the problem is that I'm trying to do this in two files (one is my tableau trusted file, the other is the actual php file).
(My problem isn't the code. I think the code works. The problem is with the procedure which isn't explained in the dupe.)
What am I doing wrong? What order should I do things? How can I get the cookie stored before I call it with PHP?
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var settings = {
url: "https://harmelin.okta.com/api/v1/users/me",
type: 'GET',
dataType: 'json',
contentType: 'application/json',
xhrFields: {
withCredentials: true
},
success: function (data) {
// alert(JSON.stringify(data));
},
error: function(err){
// alert(JSON.stringify(err));
}
}
jQuery.ajax(settings).done(function (success) {
console.log(success);
var raw = success.profile.login;
var email = raw.toLowerCase();
var oktaLogin = email.replace(/@[^@]+$/, '');
jQuery("#write-data").append(oktaLogin);
});
$(document).ready(function () {
createCookie("cookielogin", oktaLogin, "30");
});
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
}
</script>
</head>
<body>
<?php
// Tableau-provided functions for doing trusted authentication
require_once 'tableau_trusted_new.php';
?>
<div id="write-data"></div>
<?php
$user = 'jfedorowicz';
$server = 'dashboard1.harmelin.com';
$view = 'views/PTOStuff/Dashboard1';
$thelogin = $_COOKIE["cookielogin"];
echo '<iframe src="';
echo get_trusted_url( $user,$server,$view,$thelogin);
echo '" width="1000" height="1000"> </iframe>';
?>
</body>