473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

using session variables in my javascript

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).

2 1896
Dormilich
8,658 Expert Mod 8TB
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
johny10151981
1,059 1GB
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

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

Similar topics

31
by: Harry Simpson | last post by:
I've come from the old ASP camp where session variables were not used. When i started using ASP.NET in 2001, I started using them again because it was ok from what I'd read. I've been merrily...
14
by: Coleen | last post by:
Hi All :-) We have an APSX application using VB.net as the code behind, which uses one or two session variables per page. These Session variables are passed to the final page and calculations...
2
by: dana lees | last post by:
Hello, I am using session variables in a cs class file. I am inserting a sortedList to a session variable like this: System.Web.HttpContext.Current.Session = EventsSortedList; Later on,...
1
by: VMI | last post by:
When my webpage loads the first time, I fill my datatable with records from the DB and display them in my gridview. Once it loads, the user clicks on a button and add records to the datatable,...
18
by: BillE | last post by:
When a user opens a new IE browser window using File-New-Window the integrity of an application which relies on session state is COMPLETELY undermined. Anyone who overlooks the fact that...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
12
by: MrHelpMe | last post by:
Hello again all, I've finished my whole application and now I don't like the whole session variables that I am using. I have a form, user fills in info clicks submit and using CDOSYSMail an...
2
by: moorcroft | last post by:
Hi, I'm developing a web application using ASP .Net and C# as code behind. On my home page Index.aspx I have login textbox's so if you correctly enter your username and password it says "You have...
3
by: Mark Rae [MVP] | last post by:
"Lloyd Sheen" <a@b.cwrote in message news:uL5TPXPvIHA.1688@TK2MSFTNGP06.phx.gbl... Surely that will throw an exception because you're trying to populate a DataSet variable with an object...
8
by: adarshyam | last post by:
hi.. I am new to vb.net i am doing a program using dynamic textboxes which involves two pages .. where values of textboxes in one page must be transfered to to other sex of textboxes in another page,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.