1

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>
MTroy
  • 897
  • 9
  • 20
Joe Fedorowicz
  • 625
  • 2
  • 6
  • 14
  • Possible duplicate of [set cookie value in javascript and displaying it with php](http://stackoverflow.com/questions/12381573/set-cookie-value-in-javascript-and-displaying-it-with-php) – gforce301 May 16 '17 at 15:18
  • Unclear issue! are you trying to read cookie from js to php ? – MTroy May 16 '17 at 15:20
  • I'm trying to figure out how to do the following things in order: 1) Save javascript parameter in cookie 2) Call parameter in cookie with php 3) Pass parameter into php – Joe Fedorowicz May 16 '17 at 15:21
  • in the right , you should use something like : `return json_encode($_COOKIE['cookielogin']);` but since you are echoing the iframe in same time... it fail. You must improve the request control and a minimum separation in your php outputing. This is ugly – MTroy May 16 '17 at 15:30
  • I dont really need pretty. I'm not a programmer. Just need it to work. – Joe Fedorowicz May 16 '17 at 15:31
  • First, It does not work like that on SO. too many people will come to read issue to solve their own mistake (close or not) and do not care that you are not a programmer . Secondly, my point is not to be pretty, but is to make it work ! You MUST separate your outputs to make a valid json in your javascript statement – MTroy May 16 '17 at 20:44
  • can you show the javascript inside the iframe ? – MTroy May 16 '17 at 20:49

0 Answers0