Jump to content
Hooch

Session from PHP through JS?

Recommended Posts

Hey all.Is it possible to have a JS file carry my sessions?I have 1 important session that gets lost when I update something via JS.Thank you(I'm Googling for answers still)

Share this post


Link to post
Share on other sites

javascript is client side, but php is server side.You can send session to javascript easily by using php tag and echo in javascript such as var session=<?php echo $_SESSION['xxx']; ?>; .

Share this post


Link to post
Share on other sites

Now how would I add the session to a link?Here is the code in the .js file

window.location.href='page.php';

I would like to have something like

window.location.href='page.php?t2=<?php echo $_SESSION['xxx']; ?>';

This returns the link exactly as you see it. It's not echo'ing the session.Thank you for your time.

Edited by Hooch

Share this post


Link to post
Share on other sites
$_SESSION['xxx']

will get a variable "xxx" that's part of the current PHP session... do you have such a variable?BTW, what you're trying to do is probably a bad idea... what exactly are you trying to do? There's perhaps a better way to deal with it altogether.

Share this post


Link to post
Share on other sites

Thanks boen.I will try to explain my problem.I have paid someone to create a project for me that involves draggingimages in a set admin area. I then save the current positions of all theimages and then use this to display on the front end. Now comes the hard part.The current set up works great for retrieving from 1 table in the database.I want to make this more dynamic. Plus to make this even more difficultI have the option to add and subtract categories of images.What I have so far:The php part is pretty much done. I can select from any category created.I have 4 javascript files that are used in the static version. I want to make2 of these dynamic, so all my categories will use the same (4) .js files.I have the dynamic selection of categories in 2 sessions (on the php pages).

$_SESSION['t2'] = $_GET['t2'];$_SESSION['t3'] = $_GET['t3'];

These same sessions will be used to update tables from the .js files and also used to link back to theoriginating php page.This is where

window.location.href='page.php?t2=<?php echo $_SESSION['xxx']; ?>';

Came from. So it actually should be

window.location.href='page.php?t2=<?php echo $_SESSION['t2']; ?>&t3=<?php echo $_SESSION['t3']; ?>';

I hope it's a little more clear.Thank you

Edited by Hooch

Share this post


Link to post
Share on other sites

If you name your .js files as .php then the PHP code will run. PHP code will not run in a .js file unless you configure the server to do that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×