1

I have a PHP page that loads a session variable:

$user_id = $_SESSION['USER_ID'];

Previously, I included my Javascript/jQuery within that page, and added <? echo $user_id; ?> to set the Javascript variable:

$(document).ready(function() {

     $(".button").click(function() {
        var user_id = <? echo $user_id; ?>
        var dataString = 'user_id=' + user_id;  
        $.ajax({  
          type: "POST",  
          url: "../add_user.php",  
          data: dataString,  
        });  
        return false
     });


});

However, I'd like to move my Javascript to a separate page and call the script from my PHP page:

<script src="add_user.js" type="text/javascript"></script>

If I do this, I can no longer user <? echo $user_id; ?>, so what is the best way to pass my PHP variable into the Javascript/jQuery function?

Michael
  • 2,276
  • 15
  • 49
  • 80
  • 1
    You can have the JavaScript file be processed by PHP. For example, name the file `add_user.php`, and then do ``. – gen_Eric Dec 23 '11 at 16:12
  • I suppose you should use some global javascript variable inside your php-generated page or at least use a hidden field in which you can save your session_id value (and then access it from javascript) – andreapier Dec 23 '11 at 16:14
  • Why not use the href attribute instead? add the full url to the link and just use that. – Ignas Dec 23 '11 at 16:14

4 Answers4

9

You can configure your webserver to treat .js files as PHP scripts, which would let you execute your PHP code from within the .js file.

However, if that's not acceptable, you can always do something like:

<script type="text/javascript">var user_id = <?php echo json_encode($user_id) ?>;</script>
<script type="text/javascript" src="add_user.js"></script>

to define the variable at your PHP script level, and then include the external .js as usual.

If $user_id is always numeric, then the json_encode() bit is overkill, but I've gotten into the habit of using that everywhere I'm generating JS code dynamically. Using json_encode guarantees you're inserting a syntactically correct JS snippet.

Marc B
  • 356,200
  • 43
  • 426
  • 500
2
<script>
var user_id = <?php echo $user_id; ?>
</script>
<script src="add_user.js" type="text/javascript"></script>

Now you can use user_id in your add_user.js file. Also, I would advise against using PHP short tags.

Community
  • 1
  • 1
Aaron W.
  • 9,254
  • 2
  • 34
  • 45
  • A lack of a feature on *some servers* isn't a reason not to use it. Many hosters still run PHP 5.1, should we avoid using 5.3 features then? – a sad dude Dec 23 '11 at 16:30
  • Short tags depend on a proper configuration setting, and if you're planning on deploying a project to different servers you should just get used to using the standard ` – Aaron W. Dec 23 '11 at 16:36
1

You will still be able to reference your varible in your external js file

<script type="text/javascript">
var yourvar = <? echo $user_id; ?>;
</script>
<script src="add_user.js" type="text/javascript"></script>
Dominic Green
  • 10,142
  • 4
  • 30
  • 34
0

Just start the session on the other side. Its is also more secured, considering that JS data may be corrupted, even deliberately and may be exploited (security).

After starting the session - read the value there.

Of course, this is valid for scripts on the same vhost.

Rolice
  • 3,063
  • 2
  • 24
  • 32