Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
1  
possible duplicate of passing PHP objects to javascript –  Quentin Jun 18 '13 at 15:33

2 Answers 2

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'])); ?>;
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.