0

is there any way to save JavaScript variable into session value? Here is my code:

function changePage(newLoc)
{
  subid = newLoc.options[newLoc.selectedIndex].value
}

I need to save subid into session value.

Thanks, in advance.

3 Answers 3

1

You can write cookies using JavaScript. Quirksmode has a really good write up on these: http://www.quirksmode.org/js/cookies.html

This will save you the need of having to do anything server side.

You can also look at the HTML5 implementation of DOM storage such as localStorage or sessionStorage: https://developer.mozilla.org/en/DOM/Storage

Sign up to request clarification or add additional context in comments.

Comments

0

You will need to use AJAX. I would advise you to use jQuery.

function changePage(newLoc)
{
   subid = newLoc.options[newLoc.selectedIndex].value

    $.ajax({
       url: 'ajac.php',
       data: {"subId":subid},
       type: 'post',
       success:function(data){
           //Succes!
       }
    });

}

And in PHP:

$_SESSION['subId'] = $_POST['subId'];

http://api.jquery.com/jQuery.ajax/

4 Comments

Do not use jQuery just for one simple ajax statement!
If you're not familiar with AJAX it makes your life a lot easier. If he want the optimize the code later he can always write a seperate AJAX function.
ok fine i use this session varible into php so when will destory this session
Excuse me? I don't understand your question.
0

You can save values to the server

  • by using an independent request every time the value changes (which would mean ajax)
  • or by modifying the request you send to the server when changing your location (only when you navigate to a page on your own site, of course)

I would use cookies for that. They can be read and saved by both local (js) and serverside (php) script, and are exchanged each time you load your page (the second method).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.