3

I'm tring to pass a session variable to javascript but not have success. Searching stackoverflow i have found this discussion: Passing Session variables to javascript

in my custom.js i have on header

<?php session_start(); ?>

.. /// js code

strActionPage = CurrentPath + "upload_file.php?ation=store&session_user_id=<?php echo $_SESSION['session_user_id']; ?>   "; //the ActionPage's file path

but i cant get session variable from upload_file.php page

where do I wrong?

Tks to all

Community
  • 1
  • 1
user2112020
  • 97
  • 2
  • 8

3 Answers3

1

Wait, you're using PHP on a JavaScript file (that's what your OP says)? Not gonna work. You're going to have to pass it to a JavaScript function as a parameter like so:

<?php
session_start();

// HTML and stuff

<script type="text/javascript" src="custom.js"></script>
<script type="text/javascript">
passSession("<? echo $_SESSION['session_user_id']; ?>");
</script>
Descartes
  • 174
  • 1
  • 10
0

It's hard to say without knowing more about your particular error, but I did notice what seems like a typo in the code you posted:

strActionPage = CurrentPath + "upload_file.php?ation=store&session_user_id=<?php echo $_SESSION['session_user_id']; ?>   "; //the ActionPage's file path

You have ation-store -- did you mean to write action=store?

Jonah
  • 15,806
  • 22
  • 87
  • 161
0

This is how I might do.

   <script type="text/javascript">

    var user_id=<?php echo $_SESSION['session_user_id']; ?>;

    strActionPage = CurrentPath + "upload_file.php?action=store&session_user_id="+user_id;  

    </script>

There is also a possible mistake. You are using "ation" . Should that be 'action'?

Wayne Tun
  • 590
  • 7
  • 24