edhasted
asked on
PHP passing variables to JavaScript
I have read various postings on this subject and am uncertain whether this can/cannot be done.
My code is PHP.
I have a Countdown clock which is JS powered.
However if the user alters a time setting I want the countdown to alter accordingly.
So if my sessions variable for year is given by $_SESSION['Year'].
I have tried to transfer this in the JS along the lines of...
var year = '<%=Session["Year"].ToStri ng() %>'; or
var year = '<%=Session["$_SESSION['Ye ar']"].ToS tring() %>';
Have I got my syntax incorrect or is this impossible?
I would prefer not to have to pass this via the URL.
My code is PHP.
I have a Countdown clock which is JS powered.
However if the user alters a time setting I want the countdown to alter accordingly.
So if my sessions variable for year is given by $_SESSION['Year'].
I have tried to transfer this in the JS along the lines of...
var year = '<%=Session["Year"].ToStri
var year = '<%=Session["$_SESSION['Ye
Have I got my syntax incorrect or is this impossible?
I would prefer not to have to pass this via the URL.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Php?
Are you writing it in php or asp?
Your code is a mix of those two syntaxes.
Also you would have to have short tags on inorder to use this <?= where you have <%=.
There are a could of ways to do this either echo out the entire statement using php where you do
echo "var year = '".Session["Year"].ToStrin g()."'; ";
or do it like this
var year = '<?php echo Session["Year"].ToString() ; ?> ';
Your code is a mix of those two syntaxes.
Also you would have to have short tags on inorder to use this <?= where you have <%=.
There are a could of ways to do this either echo out the entire statement using php where you do
echo "var year = '".Session["Year"].ToStrin
or do it like this
var year = '<?php echo Session["Year"].ToString()
woops did not look closely enough but the way to access the session information is using $_SESSION alone you don't need the Session and the ToString().
You guys are right, the questioner is mixing ASP and PHP in a way that won't work.
ASKER
OK, let's reword this.
If I have a session designated by, $_SESSION['Year']
Can I retrieve that in the JavaScript? Yes or No.
And if Yes what is the approved syntax.
Apologies for mixing the syntax, I was copying from some proevious examples. I am using PHP.
If I have a session designated by, $_SESSION['Year']
Can I retrieve that in the JavaScript? Yes or No.
And if Yes what is the approved syntax.
Apologies for mixing the syntax, I was copying from some proevious examples. I am using PHP.
Yes, you can do it..
simple way is like below..
<script type="text/javascript">
<?php
echo "var yearNow = '".$_SESSION['Year']."';";
?>
</script>
simple way is like below..
<script type="text/javascript">
<?php
echo "var yearNow = '".$_SESSION['Year']."';";
?>
</script>
ASKER
<?php echo "var year =2011;" ?> <-- Works
<?php echo "var yearNow = '".$_SESSION['Year']."';"; ?> <-- Does not
Just working with the syntax...
<?php echo "var yearNow = '".$_SESSION['Year']."';";
Just working with the syntax...
ASKER
Sorry, forgot the bleenin' obvious...
<?php session_start(); ?>
<?php echo "var year =".$_SESSION['Year'].";" ?>
That now works perfectly. Note the minor change to the syntax.
<?php session_start(); ?>
<?php echo "var year =".$_SESSION['Year'].";" ?>
That now works perfectly. Note the minor change to the syntax.
ASKER
Please look at the syntax alteration in my last posting but essentially this is the answer. Very many thanks.
use directly the cookie/session on the client side, check set/get cookies here : http://www.w3schools.com/JS/js_cookies.asp
Open in new window