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?
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?
-
1
You can have the JavaScript file be processed by PHP. For example, name the file
add_user.php
, and then do<script src="add_user.php" type="text/javascript"></script>
. – gen_Eric Commented Dec 23, 2011 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 Commented Dec 23, 2011 at 16:14
- Why not use the href attribute instead? add the full url to the link and just use that. – Ignas Commented Dec 23, 2011 at 16:14
4 Answers
Reset to default 9You 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.
<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.
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>
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.