Welcome to WebmasterWorld Guest from 54.221.9.209

Forum Moderators: open

Message Too Old, No Replies

Altering a PHP $ Session var via Javascript?

Can it be done?

     
4:52 am on Feb 13, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 20, 2004
posts: 615
votes: 0


Hello All -

I've got a scenario where I would like to alter a PHP $_SESSION array via Javascript. It's like this:

1. PHP page is displayed containing a number of form elements for a person's name, position, hair color, etc.

2. During page display, a PHP $_SESSION array is set: $_SESSION['TSets'] = array('RegInfo');

One of the form elements on this page is a checkbox. When the checkbox is clicked a fieldset is shown via Javascript.

Now IF this checkbox is clicked, I need to alter the $_SESSION array shown above from $_SESSION['TSets'] = array('RegInfo'); to $_SESSION['TSets'] = array('RegInfo','CreditCard');

Conversely, if someone has already clicked the check box, then changes their mind and un-clicks it, I need to once again alter the session from $_SESSION['TSets'] = array('RegInfo','CreditCard'); back to simply $_SESSION['TSets'] = array('RegInfo');

Now I know that PHP is server-side and Javascript is client-side, but since PHP sessions are persistant, I don't see why (theoretically) Javascript couldn't alter a PHP Session VAR or a session Array.

So, since I don't know how to code in Javascript my question is: A) can this be done, and B) if it can, would someone show me a little code snippet on how this can be accomplished?

Thanks to all in advance,

Neophyte

5:50 am on Feb 13, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Feb 10, 2006
posts:627
votes: 0


I am not an expert in JS so take this with grain of salt....How are you handling session info - storing it in the cookie or propagating in in URL? I know that cookie data written by PHP can be read by JavaScript ( and vice versa) [might need page reload or user to go to 'next' page in order to read data]. Same goes for query part of the url (everything after www.example.com/mypage? ...) - both languages can parse it out. Or there might be some slick AJAX ( JS) approach but again I am not an expert. Quick search on any SE for reading cookie or query string in JS will give plenty tutorial examples.

HTH