1

I am trying to get a PHP session variable into an external JS file. In the JS file, I want to read and use the value of the session variable, but also assign a new value to the Session variable.

I tried this :

php file:

<?php session_start();?>
<html>
<head>
    <title></title>
    <script src="testjs.php" type="text/javascript"></script>

</head>
<body>
<?php $_SESSION['Count'] = 1; ?>
</body>
</html>

javascript file:

<?php header("Content-type: application/javascript"); ?>

alert("I am an alert box!");
var test  = <?php echo $_SESSION['Count'] ?>;

alert(test);

<?php $_SESSION['Count'] = 2 ?>
test  = <?php echo $_SESSION['Count'] ?>;

alert(test);

But I keep getting an empty value and my chrome inspector keeps giving me this error: Uncaught SyntaxError: Unexpected token ; for this line: var test = ;

Help needed :)

3
  • You are using session before it is declared. Include your js file at the end of the file. Commented Feb 2, 2016 at 12:08
  • did not work unfortunately Commented Feb 2, 2016 at 12:10
  • Write your js code inside $(document).ready( function. Commented Feb 2, 2016 at 12:13

4 Answers 4

2

You need to start the session in the js(php) file:

<?php
session_start();
header("Content-type: application/javascript"); ?>

alert("I am an alert box!");
var test  = <?php echo $_SESSION['Count'] ?>;

alert(test);

<?php $_SESSION['Count'] = 2 ?>
test  = <?php echo $_SESSION['Count'] ?>;

alert(test);

This is because the browser makes a separate request to this file, as script tag is not the same as a php include.

NOTE interspersing php and js like this is not a good idea. Although the code above will work, it will blur the mental line between serverside (php) and client side, which can lead to broken code like the following:

var abool = false;
var test  = <?php echo $_SESSION['Count'] ?>;

if(abool){
     <?php $_SESSION['Count'] = 2 ?>
}

var test  = <?php echo $_SESSION['Count'] ?>;

alert(test);

In the above, test will = 2, because the php code is executed before the javascript, so the js if block has zero impact on the php, which is essentially:

<?php
echo $_SESSION['Count'];
$_SESSION['Count'] = 2;
echo $_SESSION['Count'];
Sign up to request clarification or add additional context in comments.

7 Comments

of course haha in a similar file I already tried the same but i put session_start below header("Content-type: application/javascript"); instead of above that is why it didn't work. Thanks steve!
can you give another suggestion then instead of interpersing php and js because i need to change the session variable
Sure, but you will need to give a few more details - under what condition do you need to update the session from js? When i user performs some kind of action?
actually its like this, i got a php file in which a user can order items.. 1 by entering text in to a textarea and secondly they can order it from another website which will returtn to me xml data in which i parse that data and put it into html fields... So the user can create multiple items which can come from multiple sources... But there will be 1 div with input fields containing the items like <input type="text" name=item[1] or item[2] etc.. these item count will be saved and pulled from a session called $_SESSION['count'], this session will be updated in php files but also in the js file
So how does javascript fit into that scenario? It seems like everything you described above would be done server side with php anyhow. Do you simply need to retrieve the session data in javascript, or do you actually need to update it from javascript?
|
1

Looking at your php file, you have a rogue ; at the end of this line:

<?php $_SESSION['Count'] = 2 ?>;

The output would just be:

;

Move the semicolon and you're fine:

<?php $_SESSION['Count'] = 2; ?>

EDIT: It looks like you're not opening the session in your JS file... You're not using require() or include() so I imagine you should start with session_start() before sending headers.

2 Comments

nope still got nothing and still getting unexected token for this: var test = <?php echo $_SESSION['Count'] ?>;
See my edit... you may need to start the session with session_start()
1

You need to call session_start(); before you access $_SESSION variables. Reference to "php" file in <script> tag makes another separate request to the server.

http://php.net/manual/en/function.session-start.php

Comments

0

you can't call php code in js file ... php will work when the file extension is php... for this you need to do : create/assign javascript variable/object in php file and access it in js file

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.