2

Whats the best way to do this?

I have this $_SESSION["sessionOpentok"] I need to set to this java script var var session_id

Security isn't an issue, its okay for the end user to be able to see this session id.

I have heard of AJAX calls, json encode, delimiters, php echo, php print.

None seems to work. echo and print do not work due to quote being in the session variable.

JSON encode encodes this the number to a null.

Any ideas. Thanks

Sammy Kumar
  • 121
  • 3
  • 15
  • 1
    possible duplicate of [passing PHP objects to javascript](http://stackoverflow.com/questions/6351949/passing-php-objects-to-javascript) – Quentin Jun 18 '13 at 15:33

2 Answers2

0

In theory, just var session_id = <?php echo json_encode($_SESSION['sessionOpentok']); ?>; should do it.

However, you mayhave an encoding problem if you're getting null. Try:

var session_id = <?php echo json_encode(utf8_encode($_SESSION['sessionOpentok'])); ?>;
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Escape the string before echoing it:

var session_id = '<?php echo addslashes($_SESSION["sessionOpentok"]) ?>';

This might not work depending on how you plan to use the session_id variable as the strings won't match.

immulatin
  • 2,118
  • 1
  • 12
  • 13