0

I have a PHP file and it holds a value in this session variable: $_SESSION['image_value'].

I want this session value in a JavaScript function. Actually, I want to match a string in my JavaScript function with this session value.

Anyway I can do this?

1
  • What do you mean with "other file"? Is there a separation between PHP and JS files? Commented Oct 22, 2009 at 14:19

2 Answers 2

4

You could use json_encode():

<script type="text/javascript">
    var image_value = <?php echo json_encode($_SESSION['image_value'], JSON_HEX_TAG); ?>;
</script>

But be aware that PHP is entirely server-side and javascript is entirely client-side. That variable will be set when the page loads and any javascript modifications to it will not be preserved in the session.

Sign up to request clarification or add additional context in comments.

Comments

1

In your javascript code, instantiate x like this:

var x = "<?=$_SESSION['image_value']?>";

then just use it to do your comparisons or whatever. Of course, the script has to be evaluated by the server first, so putting this inside a .js file will not work. You need to put this in the .php file, in a tag. Maybe to this then call a function in a js file if you need to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.