Solved

PHP passing variables to JavaScript

Posted on 2011-09-13
11
318 Views
Last Modified: 2012-05-12
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"].ToString() %>'; or
var year = '<%=Session["$_SESSION['Year']"].ToString() %>';

Have I got my syntax incorrect or is this impossible?
I would prefer not to have to pass this via the URL.




 
0
Comment
Question by:edhasted
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36531225
your session value come from the browser and CAN'T BE available before/when opening the page for the first time.

use directly the cookie/session on the client side, check set/get cookies here : http://www.w3schools.com/JS/js_cookies.asp

var year = getCookie("Year");
if(year == null) ("Year", new Date().getFullYear(), 365);

Open in new window

0
 
LVL 13

Accepted Solution

by:
ansudhindra earned 500 total points
ID: 36531233

try like this..

<script type="text/javascript">
    <?php
        echo "var yearNow = '".$_SESSION['Year']."';";
    ?>
</script>
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36531262
Php?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36531280
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"].ToString()."'; ";
or do it like this
var year = '<?php echo Session["Year"].ToString(); ?> ';
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36531301
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().
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36531341
You guys are right, the questioner is mixing ASP and PHP in a way that won't work.
0
 

Author Comment

by:edhasted
ID: 36531556
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.
0
 
LVL 13

Expert Comment

by:ansudhindra
ID: 36531597
Yes, you can do it..

simple way is like below..

<script type="text/javascript">
    <?php
        echo "var yearNow = '".$_SESSION['Year']."';";
    ?>
</script>
0
 

Author Comment

by:edhasted
ID: 36531677
<?php echo "var year =2011;" ?>  <-- Works
<?php echo "var yearNow = '".$_SESSION['Year']."';"; ?> <-- Does not

Just working with the syntax...

0
 

Author Comment

by:edhasted
ID: 36531739
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.

0
 

Author Closing Comment

by:edhasted
ID: 36531750
Please look at the syntax alteration in my last posting but essentially this is the answer. Very many thanks.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help with this homework problem. 5 62
Read text on Table 7 30
Can't select treeview node from iframe 2 39
How do i use the await event in php echo 5 19
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question