By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

using session variables in my javascript

P: 25
I'm relatively new to coding so forgive if this is dumb. I have a page built in html/css/js/php. I want to add in some user functionality. I figured out how add a simple login mechanism, start sessions and carry variables, such as user's name/id, between pages.

My question is twofold:

a. Sessions being a php concept does every page I want to use session data in have to be a .php page? Everything I have written so far is in html/javascript.

b. Assuming I come to a page after starting a session and have a session variable stored in $_SESSION['userid'].. how can I then use this variable (conceptually I'd like to make an xmlhttp request into a database to pull in data that is relevant to this user). However how do I convert a php variable into a javascript variable? Please don't yell at me about the differences between server side and client side scripting.. I get it.. but there's gotta be a way I can use php variables to dynamically update my content outside of the php brackets?

<?php $_SESSION["userid"]; ?>
Apr 4 '12 #1

✓ answered by Dormilich

a) yes

b) if itís about updating HTML content, use said AJAX. otherwise print out the variable (mind that complex data types canít be sent over as is).

Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
a) yes

b) if itís about updating HTML content, use said AJAX. otherwise print out the variable (mind that complex data types canít be sent over as is).
Apr 5 '12 #2

100+
P: 1,059
We all have to do several trick to do some of our required purpose. Here is a simple example.

Expand|Select|Wrap|Line Numbers
  1. //Say you have pulled 
  2. $x=array( 1, 2,354,53,23,34)
  3. $JavascriptData="";
  4. for($i=0;$i<count($x)-1;$i++)
  5. {
  6. $JavascriptData.="\"".$x[$i]."\","; 
  7. }
  8. $JavascriptData.="\"".$x[$i]."\"";
  9. ?>
  10. <SCRIPT>
  11. var testArray={<?php echo $JavascriptData;?>};
  12. </SCRIPT>
  13. <?php
  14. ?>
  15.  
in the above example you got your an javascript array...

i hope you would figure out your way
Apr 6 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.